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->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!
Answer from Daniel W. on Stack OverflowPHP. Is it possible to use array_column with an array of objects - Stack Overflow
Using array_column in a multidimensional array.
In php is there a function like array_column for multidimensional arrays - Stack Overflow
What is the performance difference between column arrays and point source PA speakers?
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->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!
Is it possible to pass in array_column an array of objects?
PHP 7
Yes, see http://php.net/manual/en/function.array-column.php
PHP 5 >= 5.5.0
In PHP 5 array_column does not work with an array of objects. You can try with:
// object 1
$a = new stdClass();
$a->my_string = 'ciao';
$a->my_number = 10;
// object 2
$b = new stdClass();
$b->my_string = 'ciao b';
$b->my_number = 100;
// array of objects
$arr_o = array(
b);
// using array_column with an array of objects
$result = array_column(array_map(function($o){return (array)$o;},$arr_o),'my_string');
PS: for clarity I prefer to not use array_column and use array_map with an anonymous function
$result = array_map(function($o){ return $o->my_string; }, $arr_o);
or a simple foreach
$result = array();
foreach($arr_o as $o) {
$result[] = $o->my_string;
}
No, there is not a function to get your expected output natively, though you can make your own functions, just use array_column to get the types/column, and then loop over your array, on match remove the item as to not duplicate iterations.
Something like:
<?php
$data = [
['foodType' => 'fruits', 'itemID' => 1, 'itemName' => 'apple'],
['foodType' => 'fruits', 'itemID' => 2, 'itemName' => 'banana'],
['foodType' => 'veggies', 'itemID' => 3, 'itemName' => 'carrot'],
['foodType' => 'veggies', 'itemID' => 4, 'itemName' => 'broccoli']
];
function array_column_multi ($array, $column) {
$types = array_unique(array_column($array, $column));
$return = [];
foreach ($types as $type) {
foreach ($array as $key => $value) {
if ($type === $value[$column]) {
unset($value[$column]);
$return[$type][] = $value;
unset($array[$key]);
}
}
}
return $return;
}
print_r(array_column_multi($data, 'foodType'));
https://3v4l.org/KQVeN
Result:
Array
(
[fruits] => Array
(
[0] => Array
(
[itemID] => 1
[itemName] => apple
)
[1] => Array
(
[itemID] => 2
[itemName] => banana
)
)
[veggies] => Array
(
[0] => Array
(
[itemID] => 3
[itemName] => carrot
)
[1] => Array
(
[itemID] => 4
[itemName] => broccoli
)
)
)
Oh I just noticed that you're aggregating them by ID. There's not a function for that, you're going to need to iterate over the input with a loop, and populate an output array with the data you want. Eg:
$output = [];
foreach($input_array as $item) {
$output[$item['id']][] = [
'itemID' => $item['itemID'],
'itemName' => $item['itemName']
];
}