loop through array of arrays of arrays php

Zain Deeb
$myDecodedArray = json_decode($mystring,true);
var_dump($myDecodedArray) ;

I used json_decode on a string then used var_dump to help me understand the structure of my array "myDecodedArray".I got the following

array(2) { 

        ["red"]=> array(1) 
                { ["10729,10730,10732"]=> array(13) 
                        { 
                            ["-2.75"]=> array(2) { [0]=> string(5) "+1.75" [1]=> string(5) "+1.50" }
                            ["-2.50"]=> array(3) { [0]=> string(5) "+2.00" [1]=> string(4) "0.00" [2]=> string(5) "-0.50" }
                            ["-2.25"]=> array(3) { [0]=> string(5) "+2.00" [1]=> string(5) "-1.75" [2]=> string(5) "-2.00" } 
                            ["-2.00"]=> array(3) { [0]=> string(5) "+2.00" [1]=> string(5) "-2.00" [2]=> string(5) "-2.25" } 
                            ["-1.75"]=> array(3) { [0]=> string(5) "+2.00" [1]=> string(4) "0.00" [2]=> string(5) "-2.25" } 
                            ["-1.50"]=> array(3) { [0]=> string(5) "+2.00" [1]=> string(4) "0.00" [2]=> string(5) "-2.25" }
                            ["-1.25"]=> array(1) { [0]=> string(5) "-2.25" } 
                            ["-1.00"]=> array(2) { [0]=> string(5) "+2.00" [1]=> string(5) "-2.25" } 
                            ["-0.75"]=> array(2) { [0]=> string(5) "+2.00" [1]=> string(5) "-2.25" } 
                            ["-0.50"]=> array(2) { [0]=> string(5) "+2.00" [1]=> string(5) "-2.25" } 
                            ["-0.25"]=> array(1) { [0]=> string(5) "-2.25" } 
                            ["0.00"]=> array(3)  { [0]=> string(5) "+1.75" [1]=> string(4) "0.00" [2]=> string(5) "-2.25" } 
                            ["+0.25"]=> array(2) { [0]=> string(5) "-2.00" [1]=> string(5) "-2.25" }
                        } 
                } 

        ["gray"]=> array(1) 
                { ["10730,10731"]=> array(8) 
                        { 
                            ["-1.00"]=> array(4) { [0]=> string(5) "+0.25" [1]=> string(4) "0.00" [2]=> string(5) "-0.25" [3]=> string(5) "-0.50" } 
                            ["-0.75"]=> array(5) { [0]=> string(5) "+0.75" [1]=> string(5) "+0.50" [2]=> string(5) "+0.25" [3]=> string(5) "-0.50" [4]=> string(5) "-0.75" } 

                            ["-0.50"]=> array(2) { [0]=> string(5) "+0.75" [1]=> string(5) "-0.75" } 
                            ["-0.25"]=> array(2) { [0]=> string(5) "+0.75" [1]=> string(5) "-0.75" } 
                            ["0.00"]=> array(11) { [0]=> string(5) "+3.25" [1]=> string(5) "+3.00" [2]=> string(5) "+2.75" [3]=> string(5) "+2.50" [4]=> string(5) "+2.25" [5]=> string(5) "+1.00" [6]=> string(5) "+0.75" [7]=> string(5) "-0.50" [8]=> string(5) "-0.75" [9]=> string(5) "-1.50" [10]=> string(5) "-1.75" } 

                            ["+0.25"]=> array(2) { [0]=> string(5) "+1.00" [1]=> string(5) "-0.50" } 
                            ["+0.50"]=> array(2) { [0]=> string(5) "+1.00" [1]=> string(5) "-0.50" } 
                            ["+0.75"]=> array(2) { [0]=> string(5) "+1.00" [1]=> string(5) "-0.50" }
                        } 
                } 
    }

what I got is a array of arrays of arrays of arrays (4 levels). I want to loop through each array of them and read the information inside it. here is my code so far:

function read_array($arrayOfArrayes, $level = 0) {
   if (is_array($arrayOfArrayes)) {
       echo ' * '.$arrayOfArrayes.' (level: '.$level.')<br>';
   }
   elseif (!is_array($arrayOfArrayes)) {
       echo $arrayOfArrayes.' (level: '.$level.')<br>';
   }
  $level++;
   foreach ($arrayOfArrayes as $subArray) {
      read_array($subArray, $level);
     }
 }

 read_array($myDecodedArray);

here the output of my function. the function looping through the array of arrays and showing me each level but it shows the information only for the last level "4" but I want the information inside each level. I know that is happening because of (!is_array) condition. But I don't know how to do it in another way. Thanks in advance.

OwChallie

You were practically there. Get the key/value pair in the foreach loop so you can identify the top level array and output it. In order to list the rest in order, call the function recursively directly after you output the key. You also don't need to explicitly do a !is_array check as that is implied after the is_array check.

function read_array($arrayOfArrayes, $level = 0) {
  foreach ($arrayOfArrayes as $key => $val) {
    if (is_array($val)) {
      echo ' * '.$key.' (level: '.$level.')<br>';
      read_array($val, ++$level);
    }
    else {
      echo $val.' (level: '.$level.')<br>';
    }
  }
}

read_array($myDecodedArray);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP loop through arrays inside array

From Dev

How to loop through array of multiple arrays in php

From Dev

Loop through 2 arrays containing array and array of objects - Php

From Dev

Lodash loop through an array of arrays

From Dev

How to loop through an array of arrays?

From Dev

Loop through nested arrays PHP

From Dev

PHP- Loop through multidimensional array and reduce arrays if key found

From Dev

loop through array elements as strings and convert them to sub arrays php

From Dev

Angular *ngFor loop through an array of arrays

From PHP

Loop through array of arrays and extract data by id

From Dev

Loop through array of arrays in D3

From Dev

Recursively loop through array of arrays using functional

From Dev

Casting Syntax to Loop Through Array of Arrays in Swift

From Dev

Loop through array of arrays containing objects

From Dev

Loop through array of arrays and combine arrays depending on elements

From Dev

php loop through multiple arrays with same key

From Dev

Loop through JSON arrays using PHP

From Dev

Searching through arrays and Creating a custom array in PHP

From Dev

How to get the name of each array as you loop through an array of arrays php

From Dev

Loop through arrays manually

From Dev

How do I select an array to loop through from an array of arrays?

From Dev

Loop through array of arrays and increment values into a new array

From Dev

PHP iterating through arrays

From Dev

Loop through array and output links based on number of arrays

From Dev

loop through an array and compare object values to combine respective arrays

From Dev

Javascript: Loop Through Two Arrays and Sum Elements in Array Via Function

From Dev

Referencing arrays through dictionary of array names in for each loop

From Dev

Loop through array of objects, and combine arrays within objects

From Dev

*ngFor loop through an array of arrays Angular6

Related Related

  1. 1

    PHP loop through arrays inside array

  2. 2

    How to loop through array of multiple arrays in php

  3. 3

    Loop through 2 arrays containing array and array of objects - Php

  4. 4

    Lodash loop through an array of arrays

  5. 5

    How to loop through an array of arrays?

  6. 6

    Loop through nested arrays PHP

  7. 7

    PHP- Loop through multidimensional array and reduce arrays if key found

  8. 8

    loop through array elements as strings and convert them to sub arrays php

  9. 9

    Angular *ngFor loop through an array of arrays

  10. 10

    Loop through array of arrays and extract data by id

  11. 11

    Loop through array of arrays in D3

  12. 12

    Recursively loop through array of arrays using functional

  13. 13

    Casting Syntax to Loop Through Array of Arrays in Swift

  14. 14

    Loop through array of arrays containing objects

  15. 15

    Loop through array of arrays and combine arrays depending on elements

  16. 16

    php loop through multiple arrays with same key

  17. 17

    Loop through JSON arrays using PHP

  18. 18

    Searching through arrays and Creating a custom array in PHP

  19. 19

    How to get the name of each array as you loop through an array of arrays php

  20. 20

    Loop through arrays manually

  21. 21

    How do I select an array to loop through from an array of arrays?

  22. 22

    Loop through array of arrays and increment values into a new array

  23. 23

    PHP iterating through arrays

  24. 24

    Loop through array and output links based on number of arrays

  25. 25

    loop through an array and compare object values to combine respective arrays

  26. 26

    Javascript: Loop Through Two Arrays and Sum Elements in Array Via Function

  27. 27

    Referencing arrays through dictionary of array names in for each loop

  28. 28

    Loop through array of objects, and combine arrays within objects

  29. 29

    *ngFor loop through an array of arrays Angular6

HotTag

Archive