如何在不知道PHP数组深度的情况下使用foreach



我有一个像这样的大数组

 Array
    (
        [Prosess 1_35] => Array
            (
                [AF Decom_582] => Array
                    (
                        [documents] => Array
                            (
                            )
                    )
                [documents] => Array
                    (
                        [0] => stdClass Object
                            (
                                [title] => 
                                [doc_id] => 726
                            )
                        [1] => stdClass Object
                            (
                                [title] => Brunvollquizen
                                [doc_id] => 878
                            )
                        [2] => stdClass Object
                            (
                                [title] => Nytt dokument
                                [doc_id] => 1200
                            )
                        [3] => stdClass Object
                            (
                                [title] => 
                                [doc_id] => 1215
                            )
                        [4] => stdClass Object
                            (
                                [title] => Procedure AXC-12
                                [doc_id] => 1216
                            )
                    )
            )
        [Prosess 2_2] => Array
            (
                [documents] => Array
                    (
                        [0] => stdClass Object
                            (
                                [title] => 
                                [doc_id] => 1178
                            )
                    )
            )
        [Prosess 3_3] => Array
            (
                [Delprosess 3-1_10] => Array
                    (
                      [test1_340] => Array
                     (
                        [documents] => Array
                            (
                            )
                    )
                        [documents] => Array
                            (
                            )
                    )
                [documents] => Array
                    (
                    )
            )
    )
我需要创建一个简单的数组,比如

进程1_35有4个文档和一个子文件夹,子文件夹没有文档进程2_2有1个文档进程3_3没有文档但有一个子文件夹并且需要在每个文件夹

后面添加一个空数组
    array(
    array('Prosess 1_35',NULl,NUL...)
    array('NULL','doc_name,'doc_id')
    array('NULL','doc_name,'doc_id')
    array('NULL','doc_name,'doc_id')
    array('NULL','doc_name,'doc_id')
    array('  AF Decom_582',NULL,NULL)
    array('NULL','NULL',''NULL)
    array('Prosess 2_2',NULl,NUL...)
    array('NULL','doc_name,'doc_id')
    array('Prosess 3_3',NULl,NUL...)
    array('  Delprosess 3-1_10',NULl,NUL...)
    array('    test1_340',NULl,NUL...)
)

我尝试下面的代码,但不幸的是,我无法使这个简单的数组

private static function _createArray1($requirementdocs, $level = 0)
    {
        $indent = str_repeat(" ", $level * 2);*/
        foreach($requirementdocs as $key => $sreq)
        {
            $rowarr = array();
                     $key = explode("_",$subkey);
                $rowarr[0] = $indent.$key[1];
            if ($key[0] == "documents" && !empty($sreq)) continue;
            foreach($sreq as $subkey => $req)
            {
                $rowarr[1] = $req->doc_id;
                $rowarr[2] = $req->title;
                array_push($res , $rowarr);
            }
            self::_createArray($sreq, $level+1);
        }
    }
有谁能帮我一下吗?

这就是你想要完成的吗?

$arr = [
    [
        [
            1
        ],
        [
            1
        ],
        [
            1
        ],
    ],
    [
        1
    ],
    [
        [
            1
        ],
        [
            [
                [
                    1
                ]
            ]
        ],
    ]
];
echo '<pre>'; print_r($arr); echo '</pre>';
$new = flatten($arr);
echo '<pre>'; print_r($new); echo '</pre>';

和递归方法:

function flatten($array, $depth = INF)
{
    $result = [];
    foreach ($array as $item) {
        $item = $item instanceof Collection ? $item->all() : $item;
        if (is_array($item)) {
            if ($depth === 1) {
                $result = array_merge($result, $item);
                continue;
            }
            $result = array_merge($result, flatten($item, $depth - 1));
            continue;
        }
        $result[] = $item;
    }
    return $result;
}

顺便说一下,这是Laravel助手array_flatten方法的代码。

相关内容

  • 没有找到相关文章

最新更新