开发者

PHP 2 dim Array to 1 dim array

开发者 https://www.devze.com 2023-03-17 00:17 出处:网络
Is there a built in function to do the following? $a[] = $b[0][\'foo\']; $a[] = $b[1][\'foo\'];开发者_StackOverflow中文版

Is there a built in function to do the following?

$a[] = $b[0]['foo'];
$a[] = $b[1]['foo'];开发者_StackOverflow中文版
$a[] = $b[2]['foo'];
etc..

I realize I can do something like the following:

foreach($b as $c)
{
    $a[] = $c['foo'];
}

But I am really just curious if there is some built in array function that will do this. Thanks.


In short: no.

In long: Maybe ;) Its because its not "directly built-in"

With PHP5.3

$a = array_map (function ($entry) {
  return $entry['foo'];
}, $b);

or before

$a = array_map (create_function ('$entry', 'return $entry[\'foo\'];'), $b);

At least for the second solution I would prefer the foreach-loop ;)


Perhaps the array_map function

$func = function($value) {
    return $value['foo'];
};

print_r(array_map($func, $a));
0

精彩评论

暂无评论...
验证码 换一张
取 消