I have a PHP code which runs on PHP7 but not in PHP 5, which PHP version in my server, that is my code:
Array (
[0] => stdClass Object (
[userId] => 15
[name] => name0
[userName] => hh
[centerName] => center10
)
[1] => stdClass Object (
[userId] => 16
[name] => name1
[userName] => test
[centerName] => center10
)
[2] => stdClass Object (
[userId] => 1
[name] => name2
[userName] => ll
[centerName] => center1
)
)
$ids = array_unique(array_column($results, 'centerName'));
print_r($ids);
I get what I expected in PHP7 but I get an empty array in PHP5.
How can I adapt my code to work in PHP5?
As from manual, ability to use array of objects as parameter to array_column
is added since php7.0.
In php5 you simply cannot use array_column
to get columns from array of objects. So, you need to use some other code, a simple foreach
for example:
$uniqueValues = [];
foreach ($results as $item) {
$uniqueValues[$item->centerName] = 1;
}
print_r(array_keys($uniqueValues));