将键值对数组转换为关联数组 [英] Convert array of key-value pairs into associative array
问题描述
我目前有一个数组:
[1] => Array
(
[0] => 100011
[1] => 1
)
[2] => Array
(
[0] => 100013
[1] => 1
)
[3] => Array
(
[0] => 100022
[1] => 1
)
[4] => Array
(
[0] => 100025
[1] => 1
我想获取每个数组(1、2、3、4等)的第一个子项(表示[0]),并将其放入新数组中.我知道我需要遍历并将值分配给新数组.只是不确定如何去做.
I want to take the first child item (meaning [0]) of each array (1,2,3,4,etc) and put it into a new array. I know I need to loop through and assign the value to new array. Just not sure how to do it.
最终结果将是:
$ final(新数组的名称)的值是100013、100022、100025等.
$final (name of new array) has values 100013,100022,100025,etc.
我的最终结果:
我需要将其保持相同的顺序,因为然后我将使用array array_combine ( array $keys , array $values )
来创建100013作为键,并创建1作为值,100022作为键,1作为值,100025作为键,1作为价值.
I need it kept in the same order, because I am then going to use array array_combine ( array $keys , array $values )
to create 100013 as the key and 1 as the value, 100022 as the key, 1 as the value, 100025 as the key, 1 as the value.
如果您知道更快的完成方式,将不胜感激.
If you know a quicker way to accomplish, it is appreciated.
谢谢.
推荐答案
如果我理解正确,可以通过以下操作获得最终结果:
If I understand you right, the final result can be obtained by doing:
array_combine(array_column($arr, 0), array_column($arr, 1));
或者,以更传统的方式:
Or, in a more traditional way:
$result = [];
foreach ($arr as list($key, $value)) {
$result[$key] = $value;
}
这篇关于将键值对数组转换为关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!