在PHP中迭代深层嵌套数组

时间:2016-05-12 14:43:06

标签: php json iteration prestashop

我有以JSON格式保存的数据(Prestashop) - 我需要那些数据 - 这些数据深深地嵌套在数组中。

这是功能:

public static function getAllCustomizedDatas($id_cart, $id_lang = null, $only_in_cart = true, $id_shop = null)
{
    $datas = parent::getAllCustomizedDatas($id_cart, $id_lang, $only_in_cart, $id_shop);
    var_dump($datas);
    /*
     * Iterate over $datas, you're looking for
     * [id_product][id_product_attribute][id_address_delivery][id_customization][datas]
     * Datas will contain an array of fields broken by their type. You can then decode
     * the ones that need to be decoded and return the result:
     */


    return $datas;
}

如果我var_dump $ datas我看到了这个(我将其格式化为使自己更容易阅读):

array(1) {
    [8]=> array(1) { 
        [0]=> array(1) { 
            [0]=> array(2) { 
                [22]=> array(4) { 
                    ["datas"]=> array(1) { 
                        [1]=> array(1) { 
                            [0]=> array(9) { 
                                ["id_customization"]=> string(2) "22" 
                                ["id_address_delivery"]=> string(1) "0" 
                                ["id_product"]=> string(1) "8" 
                                ["id_customization_field"]=> string(1) "2" 
                                ["id_product_attribute"]=> string(1) "0" 
                                ["type"]=> string(1) "1" 
                                ["index"]=> string(1) "2" 
                                ["value"]=> string(615) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]" 
                                ["name"]=> string(20) "Client Customization" 
                            } 
                        } 
                    } 
                    ["quantity"]=> int(2) 
                    ["quantity_refunded"]=> int(0) 
                    ["quantity_returned"]=> int(0) 
                } 
                [23]=> array(4) { 
                    ["datas"]=> array(1) { 
                        [1]=> array(1) { 
                            [0]=> array(9) { 
                                ["id_customization"]=> string(2) "23" 
                                ["id_address_delivery"]=> string(1) "0" 
                                ["id_product"]=> string(1) "8" 
                                ["id_customization_field"]=> string(1) "2" 
                                ["id_product_attribute"]=> string(1) "0" 
                                ["type"]=> string(1) "1" 
                                ["index"]=> string(1) "2" 
                                ["value"]=> string(615) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]" 
                                ["name"]=> string(20) "Client Customization" 
                            } 
                        } 
                    } 
                    ["quantity"]=> int(2) 
                    ["quantity_refunded"]=> int(0) 
                    ["quantity_returned"]=> int(0) 
                } 
            } 
        } 
    } 
} 

进入深层嵌套数组的[" value"]部分的最简单方法是什么?

这必须是相当动态的,因为取决于该用户具有的项目数量 - 数组的数量将发生变化。在这个例子中,那里有2个(每个都有2个项目在'值')。如果用户可以添加3或4或10个项目。但我只是想尝试['重视']并将该JSON转换为HTML,以便传递给它的视图。

奖励:知道一种轻松迭代JSON数据的方法吗?

JSON数据如下所示:

[
    [{
        "name": "item[1][line1]",
        "customization": "asdf"
    }, {
        "name": "item[1][line2]",
        "customization": ""
    }, {
        "name": "item[1][line3]",
        "customization": ""
    }, {
        "name": "item[1][line4]",
        "customization": ""
    }, {
        "name": "item[1][line5]",
        "customization": ""
    }, {
        "name": "item[1][line6]",
        "customization": ""
    }, {
        "name": "item[1][line7]",
        "customization": ""
    }],
    [{
        "name": "item[2][line1]",
        "customization": "asdf"
    }, {
        "name": "item[2][line2]",
        "customization": ""
    }, {
        "name": "item[2][line3]",
        "customization": ""
    }, {
        "name": "item[2][line4]",
        "customization": ""
    }, {
        "name": "item[2][line5]",
        "customization": ""
    }, {
        "name": "item[2][line6]",
        "customization": ""
    }, {
        "name": "item[2][line7]",
        "customization": ""
    }]
]

2 个答案:

答案 0 :(得分:0)

我最终只是嵌套了一堆foreach语句,直到我到达了我想要的数组。它很难看,但很实用。

然后我在JSON上做了json_decode并迭代了它。

答案 1 :(得分:0)

https://packagist.org/packages/ishworkh/multi-level-array-iterator

这可能有助于迭代深层嵌套数组。然后使用密钥或层次结构信息来过滤掉您所需的值。

让我们说这是你的数组

$array = [
    [
        [
            [
                [
                    "datas"             => [
                        [
                            [
                                "id_customization"       => "22",
                                "id_address_delivery"    => "0",
                                "id_product"             => "8",
                                "id_customization_field" => "2",
                                "id_product_attribute"   => "0",
                                "type"                   => "1",
                                "index"                  => "2",
                                "value"                  => '[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]',
                                "name"                   => "Client Customization",
                            ],
                        ],
                    ],
                    "quantity"          => 2,
                    "quantity_refunded" => 0,
                    "quantity_returned" => 0,
                ],
                [
                    [
                        [
                            [
                                "id_customization"       => "23",
                                "id_address_delivery"    => "0",
                                "id_product"             => "8",
                                "id_customization_field" => "2",
                                "id_product_attribute"   => "0",
                                "type"                   => "1",
                                "index"                  => "2",
                                "value"                  => '[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]',
                                "name"                   => "Client Customization",
                            ],
                        ],
                    ],
                    "quantity"          => 2,
                    "quantity_refunded" => 0,
                    "quantity_returned" => 0,
                ],
            ],
        ],
    ],
];

您可以创建一个这样的函数,它使用多级数组迭代器的迭代方法来遍历所有嵌套数组和 产出找到的值。

/**
 * @param array $nestedArray
 *
 * @return Generator
 */
function extractValuesFromArray(array $nestedArray):Generator
{
    // $key is the local index key, which in this case should be 'value' we are looking to filter for
    foreach(\ArrayIterator\ArrayIteratorFacade::iterate($nestedArray) as $key => $ArrayElement)
    {
        if ('value' === $key)
        {
            yield $ArrayElement->getValue();
        }
    }
}

var_dump(iterator_to_array(extractValuesFromArray($array)));

应该给出

array(2) {
  [0]=>
  string(643) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
  [1]=>
  string(643) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
}