$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.
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);