Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将包含单个元素数组的数组转换为单个深度数组_Php_Arrays - Fatal编程技术网

Php 将包含单个元素数组的数组转换为单个深度数组

Php 将包含单个元素数组的数组转换为单个深度数组,php,arrays,Php,Arrays,我想知道是否有更好的解决方案:将包含具有单个值的数组的数组转换为深度较小的数组。即 阵列打印如下: Array ( [0] => Array ( [item_1] => 5 ) [1] => Array ( [item_2] => 7 ) [2] => Array ( [item_4] => 1 ) [3] => Array (

我想知道是否有更好的解决方案:将包含具有单个值的数组的数组转换为深度较小的数组。即

阵列打印如下:

Array
(
[0] => Array
    (
        [item_1] => 5
    )

[1] => Array
    (
        [item_2] => 7
    )

[2] => Array
    (
        [item_4] => 1
    )

[3] => Array
    (
        [item_5] => 1
    )

)
并应转换为打印,如:

Array
(
[item_1] => 5
[item_2] => 7
[item_4] => 1
[item_5] => 1
)
现在,我通过以下代码实现了这一点:

$items = array(...) // retrieved from db
$counts = array();
foreach ($items as $item) {
    foreach ($item as $key => $val) {
        $counts[$key] = $val;
    }
}

是否有更好的方法将包含的数组的数组项组合到单个数组中?

未经测试,但应该可以:

$items = array(...);
$counts = array_reduce($items,"array_merge",array());

未经测试,但应有效:

$items = array(...);
$counts = array_reduce($items,"array_merge",array());

工作起来很有魅力!我必须记住,回调函数也可以是任何其他php函数,也就是说,不必是我自己编写的函数!你的工作真有魅力!我必须记住,回调函数也可以是任何其他php函数,也就是说,不必是我自己编写的函数!泰