PHP:从一组数组中删除父级数组并合并节点

18

我不擅长操作数组...给定这个结构,我想要移除顶层的数组并将所有子集合并成一个平面数组:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => hey.com
                )

            [1] => Array
                (
                    [0] => you.com
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [0] => this.com
                )

            [1] => Array
                (
                    [0] => rocks.com
                )
        )
)

转换为期望的结构:

Array
    (
        [0] => hey.com
        [1] => you.com
        [2] => this.com
        [3] => rocks.com
    )

速度至关重要 - 我们将处理成百上千的结果


你真的想要得到结果为 array(array('hey.com', 'you.com', 'this.com', 'rocks.com')) 吗? - salathe
我只需要一个扁平的数组,包含所有实际值,仅此而已。 - Jared Eitnier
请看我刚刚所做的修改。 - Jared Eitnier
2
好的。如果数组中只有一个固定(单个)项,请使用一些嵌套的foreach()循环。否则,可以使用类似于这样的方法[http://cowburn.info/2012/03/17/flattening-a-multidimensional-array-in-php/]来处理。 - salathe
RecursiveArrayIterator很棒...+1,非常感谢! - Jared Eitnier
6个回答

33
$flat = call_user_func_array('array_merge', $arr);

这将会使数组完全扁平化。它将采用您提供的示例输入,并生成您要求的期望输出。

*注意-在发布此答案后编辑了问题。问题先前要求以下期望结果:

Array
(
    [0] => Array
        (
            [0] => hey.com
            [1] => you.com
            [2] => this.com
            [3] => rocks.com
        )

)

以上array_merge()答案提供了这个功能。

请确保:

  1. 您的父数组使用数字索引
  2. 父数组至少有一个子元素,否则由于array_merge投诉没有参数,您将会得到一个php错误。

对于那些想知道它如何工作的人:

// with 
$arr = [ [1,2,3], [4,5,6] ];
// call_user_func_array('array_merge', $arr) is like calling
array_merge($arr[0], $arr[1]);

// and with 
$arr = [ [1,2,3], [4,5,6], [7,8,9] ];
// then it's like:
array_merge($arr[0], $arr[1], $arr[2]);
// and so on...

如果您使用的是php 5.6+版本,那么使用散列操作符(...)是更易读的方式来完成这个操作:
$flat = array_merge(...$arr);

如果您需要将多层级数组压平,您可以使用多个嵌套的array_merge()调用,或者如果您想要递归地完全压平结构:
// This is a great option if you don't know what depth the structure may be,
// or if the structure may contain different arrays with different depths.
$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($arr)));

23

您可以使用RecursiveArrayIterator

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
$list = iterator_to_array($it,false);
var_dump($list);

输出

array (size=4)
  0 => string 'hey.com' (length=7)
  1 => string 'you.com' (length=7)
  2 => string 'this.com' (length=8)
  3 => string 'rocks.com' (length=9)

查看简单演示


在这种情况下不需要循环,请参见我的评论,上面的链接 - salathe
2
iterator_to_array 的好用之处 + - Baba
1
我对发帖者的建议是不要使用iterator_to_array(),而是直接遍历迭代器。 - salathe
1
到目前为止最好的解决方案,谢谢,它对我非常有效 :) - thednp
1
解决了我的问题。非常感谢! - user3293145

2
<?php
//Very simple recoursive solution
$array = array(
    array(
        array('hey.com'),
        array('you.com')
    ),
    array(
        array('this.com'),
        array('rocks.com'),
        array(
            array('its.com'),
            array(
                array('soo.com'),
                array('deep.com')
            )
        )
    )
);

function deepValues(array $array) {
    $values = array();
    foreach($array as $level) {
        if (is_array($level)) {
            $values = array_merge($values,deepValues($level));
        } else {
            $values[] = $level;
        }
    }
    return $values;
}

$values = deepValues($array);
echo "<pre>";
print_r($values);
echo "</pre>";
?>

我不知道如何获得这样的数组,但是这个解决方案只能获取值。
[编辑] 对不起,它更甜美了:
function deepValues(array $array, array &$values) {
    foreach($array as $level) {
        if (is_array($level)) {
            deepValues($level, $values);
        } else {
            $values[] = $level;
        }
    }
}

1
如果值始终在相同的深度级别上,您确实可以使用array_merge:
$array = [                                                                                                                                                                                                                                 
    [
        ['hey.com'],
        ['you.com'],
    ],                                                                                                                                                                                                                
    [
        ['this.com'],
        ['rocks.com'],
    ],                                                                                                                                                                                                             
];

print_r(array_merge(... array_merge(... $array)));

Getting:

Array
(
    [0] => hey.com
    [1] => you.com
    [2] => this.com
    [3] => rocks.com
)

0
如果数组仅从关联索引具有一个级别,并且只有一个元素:
foreach($arr as $key=>$val) { 
   if (is_array($arr[$key])) $arr[$key] = $arr[$key][0]; 
}

Getting:

[file] => Array (
            [name] => black.png
            [type] => image/png
            [tmp_name] => /tmp/phpfupdU5
            [error] => 0
            [size] => 197782
        )    

from:    

[file] => Array (
            [name] => Array
                ( [0] => black.png )
            [type] => Array
                ( [0] => image/png )
            [tmp_name] => Array
                ( [0] => /tmp/phpfupdU5 )
            [error] => Array
                ( [0] => 0 )
            [size] => Array
                ( [0] => 197782 )
        )

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接