I would like to get the values of inner-most arrays without using foreach. How would I get the values of $f5 and $v5 without looping through? ............................................................................................
<?php
// json_arrays.php
//
$json1 = '{"upload":[{"email":"[email protected]","deviceid":"1234567",
"crecs":[{"id":"RUS","name":"Russa"},
{"id":"AUS","name":"Austria"},
{"id":"USA","name":"United States"}]}]}';
//
echo "<br> ------- <br>";
var_dump(json_decode($json1, true));
echo "<br> ------- <br>";
var_dump(json_decode($json1));
echo "<br> ------- <br>";
$a1 = json_decode($json1, true);
//
foreach ($a1 as $f1 => $v1) {
if ($f1 == "upload") {
$a2 = $v1;
foreach ($a2 as $f2 => $v2) {
$a3 = $v2;
foreach ($a3 as $f3 => $v3) {
echo "key: ".$f3."<br>";
echo "value: ".$v3."<br>";
if ($f3 == "crecs") {
$a4 = $v3;
echo " ------- <br>";
echo "[vardump a4]"."<br>";
var_dump($a4);
echo "<br> ------- <br>";
foreach ($a4 as $f4 => $v4) {
$a5 = $v4;
foreach ($a5 as $f5 => $v5) {
echo "key: ".$f5."<br>";
echo "value: ".$v5."<br>";
}
}
}
}
}
}
}
?>
echocommands. Can we assume that they're only there for debugging, and you don't actually need them? My PHP is a bit rusty, but you can replaceforeach ($a1 as $f1 => $v1 { if ($f1 == "upload") { ... } }with$v1 = $a1["upload"]; ..., or something similar. Then do the same for the"crecs"bit. For the other loops, I'm assuming you really do need to loop through all of them, since you haven't indicated which element you're interested in.$a[2][1][0][0][0/1/2]if you want. I thought you didn't know what values you wanted for all of the keys, since you only listed"upload"and"crecs"in your code, but if you do know all their values then there's no reason you can't do this.