在经历类似的问题之后,我无法找到符合我的情景。 我有$ artists_temp数组,如
Array
(
[0] => Array
(
[ID] => 109
[post_title] => Junoon
)
[1] => Array
(
[ID] => 135
[post_title] => Linkin Park
)
)
我已经完成了代码来使数组像
Array
(
[109] => Junoon
[Junoon] =>
[135] => Linkin Park
[Linkin Park] =>
)
需要的是
Array
(
[109] => Junoon
[135] => Linkin Park
)
这是代码
$artists_temp = $wpdb->get_results($query, ARRAY_A);
$artists = array();
foreach ($artists_temp as $key => $value) {
foreach ($value as $k => $v) {
$artists[$v] = next($value);
//unset(next($value)) This doesn't work.
}
}
print_r($artists);
答案 0 :(得分:2)
PHP> = 5.5.0或使用PHP Implementation of array_column():
array_column()
由于您使用的是WordPress,请查看:wp_list_pluck():
$artists = array_column($artists_temp, 'post_title', 'ID');
答案 1 :(得分:1)
您也可以
$artists = array();
foreach ($artists_temp as $array) {
$artists[$array['ID']] = $array['post_title'];
}