PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 17, 2022

[FIXED] How to loop a tree array with unknown depth and get array blocks?

 May 17, 2022     multidimensional-array, php, tree     No comments   

Issue

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

Solution

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



Answered By - Rylee
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing