将2元素数组的数组转换为以2个元素为键=>的数组。值 [英] Convert an array of 2-element arrays to an array making 2 elements as key => value
本文介绍了将2元素数组的数组转换为以2个元素为键=>的数组。值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用某些PHP内置函数将以下数组转换为包含 id
作为键和<$ c $的值的数组c> label 作为关联值?
Is it possible to convert the following array using some PHP built-in functions to a array that contain the value of id
as key and the value of label
as the associated value? If not what's the efficient way?
谢谢。
输入数组:
Array
(
[0] => Array
(
[id] => 2
[label] => MTD-589
)
[1] => Array
(
[id] => 3
[label] => MTD-789
)
)
输出数组:
Array
(
[2] => MTD-589,
[3] => MTD-789,
)
推荐答案
介绍 array_column (仍在PHP 5.5 Beta中)。
Introducing array_column (still in PHP 5.5 Beta).
$new_array = array_column($your_array 'label', 'id');
输出:
Array
(
[2] => MTD-589,
[3] => MTD-789,
)
使用 array_walk 。
array_walk($array, function($a) use (&$return) { $return[$a['id']] = $a['label']; });
print_r($return);
这篇关于将2元素数组的数组转换为以2个元素为键=>的数组。值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文