1

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?

1

1 Answer 1

1

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()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.