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?
}
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With