Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively cycle every path of an array

I have the following(json) object:

$obj = json_decode('{
    "Group1": {
        "Blue": {
            "Round": [
                "Harold",
                "Arthur",
                "Tom"
            ]
        },
        "Green": {
            "Round": [
                "Harold"
            ],
            "Circle": [
                "Todd",
                "Mike"
            ]
        }
    },
    "Group2": {
        "Blue": {
            "Round": [
                "Peter"
            ]
        }
    }
}', true);

I'm trying to figure out how to recursively travel through it so I can see all the different paths that is in the array.

It could be 4 separate echo's or a 4 line string. The > could be replaced with anything or nothing at all. If each line was echo'd separately or pushed to an array that would probably give the most flexibility.

Group1 - Blue - Round - (Harold, Arthur, Tom)
Group1 - Green - Round - (Harold)
Group1 - Green - Circle - (Todd, Mike)
Group2 - Blue - Round - (Peter)

I can't wrap my head around it so any help would be appreciated.

I'm thinking I can somehow cycle through each like:

foreach($obj as $index => $value)
{
   // and then somehow do this until you reach an array?
}
like image 957
bryan Avatar asked Dec 08 '17 04:12

bryan


People also ask

How to find if there is a cycle in array?

Given an array arr [0..n-1] of positive and negative numbers we need to find if there is a cycle in array with given rules of movements. If a number at an i index is positive, then move arr [i]%n forward steps, i.e., next index to visit is (i + arr [i])%n.

How do you return an element from a recursive array?

Return statement: At each recursive call (except for the base case), return the minimum of the last element of the current array (i.e. arr [n-1]) and the element returned from the previous recursive call. If there is single element, return it. Else return minimum of following.

How to count cyclic elements in an array?

considering array elements in cycle. We need to count cyclic elements in the array. An element is cyclic if starting from it and moving to arr [i] + 1 leads to same element.

How to find the minimum size of an array using recursively?

Recursively find the minimum according to the following: Recursively traverse the array from the end. Base case: If the remaining array is of length 1, return the only present element i.e. arr [0] if (n == 1) return arr [0]; Recursive call: If the base case is not met, then call the function by passing the array of one size less from the end, i.e.


1 Answers

Tested only on the given sample. But this should work if array levels are increased. I mainly use RecursiveIteratorIterator class functions

// Initialize RecursiveIteratorIterator
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($obj), RecursiveIteratorIterator::SELF_FIRST);
$paths = array(); // Paths storage
foreach ($iterator as $k => $v) { // Loop thru each iterator

    if (!$iterator->hasChildren()) { // Check if iterator hasChildren is false
        $innermost = $iterator->getSubIterator($iterator->getDepth()); // Get innermost child which is the array
        for ($p = array(), $i = 0, $z = $iterator->getDepth(); $i <= $z; $i++) { // Loop and push each path to the innermost array
            $p[] = $iterator->getSubIterator($i)->key();
        }
        array_pop($p); // Remove key
        $innermost = (array)$innermost; // Cast innermost to array
        $p[] = '(' . implode(', ', $innermost) . ')'; // push formatted innermost array to path
        $path = implode(' - ', $p); // implode the path
        $paths[] = $path; // store to list of paths array
    }

}

$paths = array_unique($paths); // remove exactly same paths

foreach ($paths as $value) {  // Loop and echo each path
    echo $value.'<br>';
}

Output:- https://eval.in/915070

like image 70
Goma Avatar answered Oct 26 '22 00:10

Goma