0

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>";
                    }
                }
            }
        }
    }
}
}
?>
3
  • If you don't loop through all the possibilities, you miss most of the echo commands. 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 replace foreach ($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. Commented Apr 4, 2014 at 22:49
  • You are correct, the echo cmds are only for debugging. I did ask, "How would I get the values of $f5 and $v5 without looping through?". I was hoping to be able to use something like $a[2][1][0][0][0/1/2]? It seems from your answer that PHP doesn't support that? Thanks, Commented Apr 5, 2014 at 0:12
  • No, I'm not saying PHP is deficient. You can write $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. Commented Apr 5, 2014 at 0:31

1 Answer 1

2

You could try doing it recursively. Haven't tried it, but I think this outta do it for you.

function get_last_child_recursive($array)
{
    if(is_array(end($array))
        return get_last_child_recursive(end($array));
    else
        return array('last_value'=>end($array),'last_key'=>key($array));

}

Since the child arrays are the lest elements of each parent you can use end() to check if that element is an array, if it is - recurse - if not return the last value and last key from the array. notice value has to be returned first to move the pointer to the end of the array to return the proper key(more info on that). Haven't tested this, so it might not work, but it's a good start anyway.

Sign up to request clarification or add additional context in comments.

3 Comments

This makes sense. I also need to get all array key/value pairs in the the last array. It would be interesting to see if I could get that working and still use this function. Thanks
This seems to give a solution ~> stackoverflow.com/questions/9937137/…
@JohnD sure. In the else block, just return $array instead of what it's returning now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.