将多维PHP数组转换为二维数组

时间:2011-10-02 22:25:20

标签: php arrays multidimensional-array

我正在尝试实现我天真地认为简单的东西:展平多维数组(可能有许多嵌套级别)但结果仍然有数组。理想情况下,我正在寻找一个可以迭代10多个嵌套级别的函数,并且可以处理不同的密钥集(不一定总是相同)。

简而言之,转而:

    Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [index] => -1
                        [qty] => 77
                        [id] => 7
                    )

                [1] => Array
                    (
                        [index] => -1
                        [qty] => 83
                        [id] => 8
                    )
            )

        [1] => Array
            (
                [0] => Array
                    (
                        [index] => -1
                        [qty] => 75
                        [id] => 13
                    )

                [1] => Array
                    (
                        [index] => -1
                        [qty] => 60
                        [id] => 14
                        [msr] => g
                    )
            )

        [2] => Array
            (
                [index] => -1
                [qty] => 10
                [id] => 12
            )
    )

进入这个:

    Array
    (
        [0] => Array
            (
                [index] => -1
                [qty] => 77
                [id] => 7
            )

        [1] => Array
            (
                [index] => -1
                [qty] => 83
                [id] => 8
            )

        [2] => Array
            (
                [index] => -1
                [qty] => 75
                [id] => 13
            )

        [3] => Array
            (
                [index] => -1
                [qty] => 60
                [id] => 14
                [msr] => g
            )

        [4] => Array
            (
                [index] => -1
                [qty] => 10
                [id] => 12
            )
    )

这就是我的想法并且可以工作,但我最终得到一个没有关键信息的平面数组(如果我想要键,每次迭代都会覆盖以前的值,最后我只得到它们的最后一个数组):

function flatten_multi_array(array $array){
    $ret_array = array();
    foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value) {
        $ret_array[] = $value;
    }
    return $ret_array;
}

4 个答案:

答案 0 :(得分:1)

function dig($source, &$out){
    foreach ($source as $k => $v){
        if (isset($v["index"]){
             $out[] = $v;
        } else {
             dig($v, $out);
        }
    }
}

就是这样。

用法:

$out = array();
$source = array(); // your magic nested array

dig($source, $out);

现在$out已满足您的需求。

答案 1 :(得分:1)

如果您仍在寻找RecursiveIteratorIterator方法,请参阅以下内容:

foreach(new RecursiveIteratorIterator(new ParentIterator(new RecursiveArrayIterator($array)), RecursiveIteratorIterator::SELF_FIRST) as $value) {
    if (isset($value['index']))
        $ret_array[] = $value;
}

这应该在你的功能中完成。另请参阅demo

相关:Quick Recursive search of all indexes within an array

答案 2 :(得分:0)

像这个未经测试的代码可能......

$outArray = array();
foreach($originalArray as $nestedArray){
    foreach($nestedArray as $innerArray){
        $outArray[] = $innerArray;
    }
}
print_r($outArray);

答案 3 :(得分:0)

我看到这只是回答了。好吧,我以为自从我做了以后我就会贡献我的解决方案。 :P

$newArray = array();
function isMulti($k,$v){
    global $newArray;
    if(is_array($v)){
    foreach($v as $k2 => $v2){
        if(!is_array($v2)){
            $newArray[] = $v;
            break;
        }else{
            isMulti($k2,$v2);
        }
    }
    }
}
foreach($arrayInQuestion as $k => $v){
    isMulti($k,$v);
}