长期困扰我的东西:
我想转换这个数组:
// $article['Tags']
array(3) {
[0] => array(2) {
["id"] => string(4) "1"
["tag"] => string(5) "tag1"
},
[1] => array(2) {
["id"] => string(4) "2"
["tag"] => string(5) "tag2"
},
[2] => array(2) {
["id"] => string(4) "3"
["tag"] => string(5) "tag3"
},
}
以这种形式:
// $extractedTags[]
array(3) {
[0] => string(4) "tag1",
[1] => string(4) "tag2",
[2] => string(4) "tag3",
}
目前我正在使用此代码:
$extractedTags = array();
foreach ($article['Tags'] as $tags) {
$extractedTags[] = $tags['tag'];
}
有没有更优雅的方式来做这个,也许是一个php内置函数?
答案 0 :(得分:1)
您可以array_map
使用anonymous functions:
// PHP 5.2
$extractedTags = array_map(create_function('$a', 'return $a["tag"];'), $article['Tags']);
// PHP 5.3
$extractedTags = array_map(function($a) { return $a['tag']; }, $article['Tags']);