Home > OS >  How to loop a tree array with unknown depth and get array blocks?
How to loop a tree array with unknown depth and get array blocks?

Time:05-14

I found this solution to loop and print an unknown depth array:

    printAllValues($arr);

    function printAllValues($arr) {
        if(!is_array($arr)) {
            echo ($arr);
            return;
        }
        foreach($arr as $k => $v) {
            printAllValues($v);
        }
    }

But this solution prints each array value individually. I want to get (id, name, is_master) at once and save into db. Not each one individually. The array to loop looks like it:

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 0ea8a4ab99694ce7b74f08ed7fc4602e
                    [name] => xxxxxxx
                    [is_master] => 
                    [master_id] => ce93cab80820446290f7ea627578e6e9
                    [has_children] => 1
                    [nodes] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 431169710c83410d841ab3cce079a0fd
                                    [name] => xxxxxxxxxxx
                                    [is_master] => 
                                    [master_id] => 0ea8a4ab99694ce7b74f08ed7fc4602e
                                    [has_children] => 
                                    [nodes] => 
                                )

                            [1] => Array
                                (
                                    [id] => 98e15adfd57c4285981781590e112d32
                                    [name] => xxxxxxxxx
                                    [is_master] => 
                                    [master_id] => 0ea8a4ab99694ce7b74f08ed7fc4602e
                                    [has_children] => 1
                                    [nodes] => Array
                                    ....

And I want to loop and save each block like this into db:

            [id] => 0ea8a4ab99694ce7b74f08ed7fc4602e
            [name] => xxxxxxx
            [is_master] => 
            [master_id] => 1234567

CodePudding user response:

If you just want to process when encountering these values then you can do this:

function printAllValues($arr) {
    if (!is_array($arr)) {
        // don't process this
        return;
    }

    if (
        isset($arr["id"]) &&
        isset($arr["name"]) &&
        isset($arr["is_master"]) &&
        isset($arr["master_id"])
    ) {
        // all array keys exist
        // process data
    }

    foreach ($arr as $k => $v) {
        printAllValues($v);
    }
}

If you want to retrieve all the values, we can adjust that further and return a "simplified" array of these values

function getAllValues($arr, $values = []) {
    if (!is_array($arr)) {
        // no modifications, return original array
        return $values;
    }

    if (
        isset($arr["id"]) &&
        isset($arr["name"]) &&
        isset($arr["is_master"]) &&
        isset($arr["master_id"])
    ) {
        // all array keys exist
        // store data
        $values[] = [
            "id" => $arr["id"],
            "name" => $arr["name"],
            "is_master" => $arr["is_master"],
            "master_id" => $arr["master_id"],
        ];
    }

    foreach ($arr as $k => $v) {
        $values = getAllValues($v, $values);
    }

    return $values;
}

$simplified = getAllValues($arr);

echo json_encode($simplified);
/* Example Output
[
    {
        "id": "1",
        "name": "1",
        "is_master": "1",
        "master_id": "1"
    },
    {
        "id": "2",
        "name": "2",
        "is_master": "1",
        "master_id": "2"
    }
]
*/

NOTE: these are simple examples trying to match your code structure, there are many different ways to do this

CodePudding user response:

Save the values of the current level to the database. Then if there are children, loop over the children and call the function recursively.

function saveAllValues($arr) {
    save_to_db($arr['id'], $arr['name'], $arr['is_master'], $arr['master_id']);
    if ($arr['has_children']) {
        array_map('saveAllValues, $arr['nodes']);
    }
}
  • Related