简体   繁体   中英

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.

$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.

$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.

Given the three inputs, the final output would look like

$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.

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.

Not sure if this will help. I wrote an apps years age so I can easily see the structure of a complex JSON file.
And I have a JSON file from a recent scraping a Zillow real estate listings.
This link gets the JSON file from a url.
In this example it gets zillow.jsn from the same location as the app.

JSON Analyzer Example

If you want you could use it to get the PHP variables you want to compare.
just change the url of the JSON file after the f=

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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