简体   繁体   English

递归遍历并比较多维数组与一维数组并使用映射值构建差异

[英]Recursively traverse and compare a multi-dimensional array with a one dimensional array and build a diff using the mapped values

Given these two arrays (one comes from json, decoded into a PHP array while the other is from a DB so it's 1 dimensional) I need to somehow compare only some of the values to build a new nested array with only the changed values.鉴于这两个 arrays (一个来自 json,解码为 PHP 数组,而另一个来自数据库,因此它是一维的)我需要以某种方式构建一个新的嵌套数组,仅与一些更改的值进行比较。

$data_array = [
    'level_one' => [
        'level_two' => [
            'level_three' => [
                'array.key.1' => "some_value_1",
                'array.key.2' => "some_value_2",
            ],
            'ordinary_key' => 'value'
        ]
    ],
    'example_1' => [
        'example_2' => [
            "color.1" => "green"
        ],
        'example_3' => [
            "color.2" => "orange",
            "color.3" => "red",
        ]
    ]
];

$db_array = [
    'array_key_1' => "db_value_1",
    'array_key_2' => "some_value_2",
    'color_1' => "pink",
    'color_2' => "orange",
    'color_3' => "yellow"
];

Since the key names I want to compare are not the same and they are also nested in the data array (eg DB = array_key_1 and Data array = level_one > level_two > level_three > array.key.1 , I am creating a third array to map the keys. It contains all parent nodes that lead to the children that need to be compared, at the end it is an array to map the key from the data/json array to the db array.由于我要比较的键名不相同,并且它们也嵌套在数据数组中(例如DB = array_key_1Data array = level_one > level_two > level_three > array.key.1 ,我正在为 map 创建第三个数组键。它包含通向需要比较的子节点的所有父节点,最后它是一个数组到 map 从 data/json 数组到 db 数组的键。

$compare = [
    'level_one' => [
        'level_two' => [
            'level_three' => [
                [ 'json_key' => 'array.key.1', 'database' => 'array_key_1'],
                [ 'json_key' => 'array.key.2', 'database' => 'array_key_2']
            ],
        ]
    ],
    'example_1' => [
        'example_3' => [
            [ 'json_key' => 'color.2', 'database' => 'color_2'],
            [ 'json_key' => 'color.3', 'database' => 'color_3']
        ]
    ]
];

I want to traverse the $compare array, find the keys that need to be compared, use the keys to access the data in the db & data array and then build a third nested array with the values from the db array if they are different.我想遍历 $compare 数组,找到需要比较的键,使用键访问 db & data 数组中的数据,然后使用 db 数组中的值(如果它们不同)构建第三个嵌套数组。

Given the three inputs, the final output would look like给定三个输入,最终的 output 看起来像

$diff_array = [
    'level_one' => [
        'level_two' => [
            'level_three' => [
                'array.key.1' => "db_value_1"
            ]
        ]
    ],
    'example_1' => [
        'example_3' => [
            "color.3" => "yellow",
        ]
    ]
];

I've been exploring RecursiveIteratorIterator and ArrayIterator but am not sure how after reaching the child node in the $compare array where the key mappings are, how to use the parent information to access the same part of the $data array to make the comparison to the $db array.我一直在探索RecursiveIteratorIteratorArrayIterator但不确定如何在到达键映射所在的$compare数组中的子节点后,如何使用父信息访问 $data 数组的相同部分以进行比较$db数组。

The $compare array can be changed as it was something I came up with as a way to identify the mapping of a nested array to non-nested. $compare数组可以更改,因为它是我想出的一种方法来识别嵌套数组到非嵌套的映射。

Not sure if this will help.不确定这是否会有所帮助。 I wrote an apps years age so I can easily see the structure of a complex JSON file.我写了一个几年前的应用程序,所以我可以很容易地看到一个复杂的 JSON 文件的结构。
And I have a JSON file from a recent scraping a Zillow real estate listings.我有一个 JSON 文件,来自最近抓取的 Zillow 房地产清单。
This link gets the JSON file from a url.此链接从 url 获取 JSON 文件。
In this example it gets zillow.jsn from the same location as the app.在此示例中,它从与应用程序相同的位置获取 zillow.jsn。

JSON Analyzer Example JSON 分析仪示例

If you want you could use it to get the PHP variables you want to compare.如果您愿意,可以使用它来获取要比较的 PHP 变量。
just change the url of the JSON file after the f=只需在 f= 之后更改 JSON 文件的 url

At the bottom of the result is a summary of the variables and the data types.结果的底部是变量和数据类型的摘要。

If there is any code you might want from this app let me know.如果您可能想从此应用程序中获得任何代码,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM