Elliot Chance
Elliot Chance

Reputation: 5736

Getting value for keys inside nested arrays

An example:

$result = [];
foreach($ret as $item) {
    $result[] = $item['id'];
}
return $result;

Is there a one-liner in PHP that can do this? It very hard to search on google for "keys in arrays" because it's very vague...

Upvotes: 0

Views: 79

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

PHP >= 5.5.0 required for array_column:

$result = array_column($ret, 'id');

Other than that your loop is as good as it gets.

Upvotes: 1

Related Questions