PHP function array_column() returns the values from a single column of the array, identified by the column_key, bug it can only used in PHP version 5.5+.
What can I do in my PHP 5.2 VPS? It’s piece of cake.
Lets start:
function my_array_column($array, $colName){
$results=array();
if(!is_array($array)) return $results;
if (function_exists('array_column')) {
return array_column($array, $colName);
}
foreach($array as $child){
if(!is_array($child)) continue;
if(array_key_exists($colName, $child)){
$results[] = $child[$colName];
}
}
return $results;
}
How to use it:
$data = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe'
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith'
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones'
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe'
)
);
$result = my_array_column($data, 'id');
So, It’s very easy.