开发者

Imploding nested arrays

If I have an array structured like this:

开发者_StackOverflow社区
$array[index]['first_name']
$array[index]['last_name']

Is there an easy way to implode it into something like first_name last_name,first_name last_name, etc. for all the indices?

Implode didn't seem to do what I wanted for something like this. Currently I'm just looping over the whole thing, but it's a SIGNIFICANT bottleneck.


function combineFirstLastName($user) {
    return $user['first_name'] . ' ' . $user['last_name'];
}

$firstLastNames = array_map('combineFirstLastName', $array);

If you're using PHP >= 5.3, you can use an anonymous function.

$firstLastNames = array_map(function($user) {
    return $user['first_name'] . ' ' . $user['last_name'];
}, $array);

I'm not sure you'll get much more of a speed improvement, though. Have you considered opcode caching?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜