I have a PHP array as:
$array = [
['key' => 'foo', 'value' => 'fooVal'],
['key' => 'bar', 'value' => 'barVal'],
];
Is there an easy way of extracting the keys so that I have ['foo', 'bar']
or I must loop through $array
?
I have a PHP array as:
$array = [
['key' => 'foo', 'value' => 'fooVal'],
['key' => 'bar', 'value' => 'barVal'],
];
Is there an easy way of extracting the keys so that I have ['foo', 'bar']
or I must loop through $array
?
You can use array_column
to get values of a single column from an array
$array = [
['key' => 'foo', 'value' => 'fooVal'],
['key' => 'bar', 'value' => 'barVal'],
];
$result = array_column( $array , 'key' );
echo "<pre>";
print_r( $result );
echo "</pre>";
This will result to:
Array
(
[0] => foo
[1] => bar
)
Doc: array_column()