According to the PHP Manual, array_column
function is available from of PHP 5.5
It returns the value of a "column" of a multidimensional array in a single array.
Example:
$records = 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',
)
);
$first_names = array_column($records, 'first_name');
print_r($first_names);
Result:
Array
(
[0] => John
[1] => Sally
[2] => Jane
[3] => Peter
)
But it is not present in versions prior to PHP 5.5 and this is a very good function to simplify an array structure when needed.
How could I develop a function that did the same thing in earlier versions of PHP?