开发者

PHP 2 dim Array to 1 dim array

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜