遍历可变深度的多维PHP数组,以在每个类别级别执行不同的功能

时间:2020-08-16 09:50:12

标签: php loops associative-array catalog

我有一个产品目录,表示为一个(大型)关联的PHP数组($ productsJSON),我正尝试将其转换为产品目录。

目录由(理论上是无限的)类别级别相互嵌套组成。


>Parent Category - has CategoryLevel set as 1. Name and text are printed.
->1st child Category - has CategoryLevel set as 2
and so on down. Name and text are printed.
-->Final child Category - has CategoryLevel set as 1, 2, 5, 9 etc and also has IsCollection equal to 'Yes'. Name and text are printed. Product table is drawn.

这是$ productsJSON数组的一部分,仅供参考。


Array
(
    [CategoryID] => 8674
    [UID] => 2
    [timestamp] => 1597620853
    [CategoryName] => Cat Aut
    [CategorySortOrder] => 1
    [CategoryLevel] => 1
    [assetType] => category
    [IsCollection] => No
    [children] => Array
        (
            [0] => Array
                (
                    [CategoryID] => 8675
                    [UID] => 5
                    [timestamp] => 1597620853
                    [CategoryName] => Test 1
                    [CategorySortOrder] => 1
                    [CategoryLevel] => 2
                    [assetType] => category
                    [IsCollection] => No
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [CategoryID] => 8677
                                    [UID] => 9
                                    [timestamp] => 1597620853
                                    [CategoryName] => Test 1A
                                    [CategorySortOrder] => 1
                                    [CategoryLevel] => 3
                                    [assetType] => category
                                    [IsCollection] => No
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [CategoryID] => 8682
                                                    [UID] => 14
                                                    [timestamp] => 1597620853
                                                    [CategoryName] => Collection 1A No1
                                                    [CategorySortOrder] => 1
                                                    [CategoryLevel] => 4
                                                    [assetType] => category
                                                    [IsCollection] => Yes
                                                    [children] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [assetType] => table
                                                                    [UID] => 14
                                                                    [timestamp] => 1597620853
                                                                    [header] => Array
                                                                        (
                                                                            [0] => Array
                                                                                (
                                                                                    [value] => ProductCode
                                                                                )

                                                                            [1] => Array
                                                                                (
                                                                                    [value] => ProductName
                                                                                )

                                                                            [2] => Array
                                                                                (
                                                                                    [value] => ProductWidth
                                                                                )

                                                                            [3] => Array
                                                                                (
                                                                                    [value] => ProductHeight
                                                                                )

                                                                            [4] => Array
                                                                                (
                                                                                    [value] => Length
                                                                                )

                                                                        )

                                                                    [widths] => Array
                                                                        (
                                                                            [0] => 2
                                                                            [1] => 10
                                                                            [2] => 2
                                                                            [3] => 2
                                                                            [4] => 2
                                                                        )

                                                                    [rows] => Array
                                                                        (
                                                                            [0] => Array
                                                                                (
                                                                                    [0] => Array
                                                                                        (
                                                                                            [value] => 00001
                                                                                        )

                                                                                    [1] => Array
                                                                                        (
                                                                                            [value] => Product 01
                                                                                        )

                                                                                    [2] => Array
                                                                                        (
                                                                                            [value] => 50
                                                                                        )

                                                                                    [3] => Array
                                                                                        (
                                                                                            [value] => 50
                                                                                        )

                                                                                    [4] => Array
                                                                                        (
                                                                                            [value] => 110
                                                                                        )

                                                                                )

                                                                            [1] => Array
                                                                                (
                                                                                    [0] => Array
                                                                                        (
                                                                                            [value] => 00002
                                                                                        )

                                                                                    [1] => Array
                                                                                        (
                                                                                            [value] => Product 02
                                                                                        )

                                                                                    [2] => Array
                                                                                        (
                                                                                            [value] => 50
                                                                                        )

                                                                                    [3] => Array
                                                                                        (
                                                                                            [value] => 50
                                                                                        )

                                                                                    [4] => Array
                                                                                        (
                                                                                            [value] => 210
                                                                                        )
                                                                            )

完整数组(作为JSON文件)可以在这里https://pastebin.com/k8vhGGS5

中找到

我正在使用$ productsJSON = json_decode($ str,true);转换为PHP数组。

单步执行树,我需要能够运行一个函数来为所有类别节点(IsCollection是是或否)添加类别名称,文本等,然后为终端级别(其中IsCollection是是)添加功能绘制产品表。 (我已经有此功能)。

我无法掌握的是如何有效地遍历目录数组而无需对各种路径进行硬编码,例如$ productsJSON ['children'] [0] ['children'] [0] ['children'] [0] ['header']遍历每个表头。

我已经尝试过嵌套搜索功能,例如How to search by key=>value in a multidimensional array in PHP


function search($array, $key, $value)
{
    $results = array();

    if (is_array($array)) {
        if (isset($array[$key]) && $array[$key] == $value) {
            $results[] = $array;
        }

        foreach ($array as $subarray) {
            $results = array_merge($results, search($subarray, $key, $value));
        }
    }

    return $results;
}

echo "<pre>";
print_r(search($productsJSON, 'assetType', 'table'));
echo "</pre>";

我也尝试过使用RecursiveIteratorIterator和RecursiveArrayIterator组合,例如Iterating over a complex Associative Array in PHP(请看第二个答案)-但这似乎完全炸开了数组,因此我无法遍历每个类别或表标题,宽度和数据行。


$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($productsJSON));

$ntree=-1;
$v=0;
$treeArray = array();
foreach($iterator as $key => $value)
{
    echo "$ntree $key - $value<br />";

    if($key === "CategoryID" || $key === "CategoryName" || $key === "CategoryLevel" || $key === "IsCollection")
    {
        if($key === "CategoryID")
        {
            $ntree++;
            $treeArray[$ntree]['CategoryID'] = $value;
        }
        if($key === "CategoryName")
        {
            $treeArray[$ntree]['CategoryName'] = $value;
        }
        if($key === "CategoryLevel")
        {
            $treeArray[$ntree]['CategoryLevel'] = $value;
        }
        if($key === "IsCollection")
        {
            $treeArray[$ntree]['IsCollection'] = $value;
        }

    }
    if($key === "assetType" && $value === "table")
    {
        $treeArray[$ntree]['assetType'] = $value;
        $treeArray[$ntree]['thisV'] = $v;
        $tableLine=1;
    }
    if($tableLine > 2)
    {
        $treeArray[$ntree]['widths'] = $value;
    }
$v++;
$tableLine++;
}

一旦有了数组,就可以在页面上获得类别标题,也不必写表头和数据行。

我整个周末都在研究Stack Overflow和各种数组教程,但我陷入了困境。非常感谢您的帮助。

(这也是我在这里的第一个问题。如果我没有尽我所能解释,请事先道歉。如果有任何反馈,我会尽力编辑问题。)< / p>

0 个答案:

没有答案