PHP. Is it possible to use array_column with an array of objects
PHP 5
array_column
doesn't work with an array of objects. Use array_map
instead:
$titles = array_map(function($e) {
return is_object($e) ? $e->Title : $e['Title'];
}, $records);
PHP 7
array_column()
The function now supports an array of objects as well as
two-dimensional arrays. Only public properties are considered, and
objects that make use of__get()
for dynamic properties must also
implement__isset()
.
See https://github.com/php/php-src/blob/PHP-7.0.0/UPGRADING#L629 -
Thanks to Bell for the hint!
array_column with an array of objects
$array = array_combine(array_map(function ($o) { return $o->id; }, $array), $array);
Whether this is really a lot better than a simple foreach
loop, aside from "but, but, functional programming...!", is debatable.
PHP, array of objects, how can I use one set of object values as the array keys
As of PHP 7, you can use array_column()
on objects, with the third parameter as the column to index by...
$questions = array_column($data, "name", "id");
array_column not working for an array of objects?
v7.0.0: Added the ability for the input parameter to be an array of objects.
Source: https://secure.php.net/manual/en/function.array-column.php
array_column doesn't work with first key in nested array
This is a recursive function which at each point checks if the key matches the required one, if so it returns the value, if not it will look in elements which are also arrays (which is where the recursive bit comes in) and return if a match is found there...
function nested_array_key_search ( array $array, $key ) {
foreach ( $array as $elementKey => $element ) {
if ( $elementKey === $key ) {
return $element;
}
if ( is_array($element) ) {
$nested = nested_array_column ( $element, $key );
// If the recursive call returns a value
if ( $nested !== false ) {
return $nested;
}
}
}
// Not found, return false
return false;
}
$bar = nested_array_key_search($array, 2);
print_r($bar);
gives...
Array
(
[5] => Array
(
[7] => Array
(
[46] => Array
(
)
)
)
[108] => Array
(
)
)
PHP - Extracting a property from an array of objects
If you have PHP 5.5 or later, the best way is to use the built in function array_column()
:
$idCats = array_column($cats, 'id');
But the son has to be an array or converted to an array
Isolate a specific column of values in an array of objects
I would suggest array_map()
.
$original_array; // your original array
$rid_array = array_map(function($val) {
return $val->rid;
}, $original_array);
Or for PHP < 5.3
$original_array; // your original array
$rid_array = array_map('map_function', $original_array);
function map_function($val) {
return $val->rid;
}
A shorthand and brief way to collaps array of objects in PHP
Yes, you can use array_column
https://www.php.net/manual/en/function.array-column.php
Get multi column from array php, Alternate of array_column
There is custom function to achieve this,
public function filterArrayByKeys(array $input, array $column_keys)
{
$result = array();
$column_keys = array_flip($column_keys); // getting keys as values
foreach ($input as $key => $val) {
// getting only those key value pairs, which matches $column_keys
$result[$key] = array_intersect_key($val, $column_keys);
}
return $result;
}
$a = $this->filterArrayByKeys($students, ['id','name','email']);
print_r($a);
array_flip — Exchanges all keys with their associated values in an array
array_intersect_key — Computes the intersection of arrays using keys for comparison
Output
Array
(
[0] => Array
(
[id] => 498
[name] => Andrew A. Blaine
[email] => student14@gmail1.com
)
[1] => Array
(
[id] => 499
[name] => Billie C. Heath
[email] => student15@gmail1.com
)
)
Working demo.
Source.
Related Topics
How to Check If an Integer Is Within a Range
Uncaught Reflectionexception: Class Log Does Not Exist Laravel 5.2
How to Generate Unique Id in MySQL
How Add Class='Active' to HTML Menu with PHP
PHP Warning: Module Already Loaded in Unknown on Line 0
Sending Images from Canvas Elements Using Ajax and PHP $_Files
PHP Parse Error: Syntax Error, Unexpected T_Object_Operator
Getting Time Difference Between Two Times in PHP
Need a Good Regex to Convert Urls to Links But Leave Existing Links Alone
Passing PHP Variable into JavaScript
What Does a Successful MySQL Delete Return? How to Check If Delete Was Successful
Title Case a String Containing One or More Last Names While Handling Names with Apostrophes
PHP Curl: I Need a Simple Post Request and Retrival of Page Example
How to Delete a Folder with Contents Using PHP
How to Remember Input Data in the Forms Even After Refresh Page
File_Get_Contents - Failed to Open Stream: Http Request Failed! Http/1.1 404 Not Found
How to Use Mamp's Version of PHP Instead of the Default on Osx