Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through php multi level associative array

I am struggling to return values from all layers of a multidimensional array, e.g. value then all child elements. I've tried lots of loops but can't quite get it right. Please can someone help - I've been stuck for ages! Thank you.

The array is:

Array
(
    [SiteRep] => Array
        (   [DV] => Array
                (
                    [Location] => Array
                        (
                            [Period] => Array
                                (
                                    [0] => Array
                                        (
                                            [value] => 2016-12-19Z
                                            [Rep] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [D] => W
                                                            [F] => 1
                                                            [G] => 9
                                                        )

                                                    [1] => Array
                                                        (
                                                            [D] => W
                                                            [F] => 0
                                                            [G] => 7
                                                        )

                                                )
                                        )

                                    [1] => Array
                                        (
                                            [value] => 2016-12-20Z
                                            [Rep] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [D] => ENE
                                                            [F] => 4
                                                            [G] => 7

                                                        )

                                                    [1] => Array
                                                        (
                                                            [D] => E
                                                            [F] => 3
                                                            [G] => 9

                                                        )

so far my code is:

$i=0;
    foreach ($json_decoded['SiteRep']['DV']['Location']['Period'][$i] as $key => $value) {
    if (is_array($value)){
        foreach ($value as $key2 => $value2){
            if (is_array($value2)){
                foreach ($value2 as $key3 => $value3) {
                    echo $key3 . " 3: " . $value3 . "<br>";
                }
            } else {
                echo $key2 . " 2: " . $value2 . "<br>";
            }
        };

    } else {
        echo $key . " 1: " . $value . "<br>";
    }
    $i++;
};
like image 333
Leonard Bassoon Avatar asked Nov 27 '25 21:11

Leonard Bassoon


1 Answers

I believe you are looking for recursion. A function that calls itself. It can be tricky but it is by far the most efficient way to navigate an array of indeterminate depth.

function printStuff($stuff)
{
  echo $stuff['value']
  if (isset($stuff['rep']))
  {
    printStuff($stuff['rep']);
  }
}

If you want the values to pass down simply change echo to a return value:

function printStuff($stuff)
{
  $temp = $stuff['value']
  if (isset($stuff['rep']))
  {
    $temp .= printStuff($stuff['rep']);
  }
  return $temp;
}

Note: recursion can cause out of memory if not set up correctly similar to an infinite loop, as the recursive function call pushes the function call onto the call stack every time. Just make sure your function call has a subset of the parameter passed in.

like image 50
danielson317 Avatar answered Nov 30 '25 12:11

danielson317



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!