1

I have an array, $json_data, I'm trying to parse with a for loop:

This is the array:

array(2) {
    ["data"]=>
    array(4) {
        ["children"]=>
        array(25) {
            [0]=>
            ["first_key"]=>
            array(50) {
                ["second_key"]=> 
                string(9) "My Title"
            }
        }
    }
}

Then in PHP, I set this as $json_data and start pulling data out with a for loop

foreach  ( $json_data['data']['children'] as $key => $value ) {
    echo $title = $value['first_key']['second_key'];
}

Outputs "My Title" as expected.

My question (and if my question is poorly worded, please advise) is, can I set the keys as a variable? I know that I can't set it as a string, but can I pass array of keys to $value like:

$key_array = array('first_key' => array('second_key'));

foreach  ( $json_data['data']['children'] as $key => $value ) {
    echo $title = $value[$key_array];
}

Or something like that?

Making a function to which I could pass the $array to a function that has the $key_array set would also be great, like:

echo get_array_value( $array, $array_keys );

but would love a shove in the right direction.

4
  • Do you need to get the data out of the array? What are you trying to do? Commented Oct 16, 2016 at 3:42
  • Yes, hence the question ;). I would like to set the "path" to the correct key/value pair before the array is created. Commented Oct 16, 2016 at 3:47
  • Is it always the same path? Commented Oct 16, 2016 at 3:49
  • No, hence my wanting to set a variable. Commented Oct 16, 2016 at 3:56

1 Answer 1

1

First of all, see here:

$key_array = array('first_key' => array('second_key'));

Instead of declaring a multidimensional array of keys, declare a simple array comprising of all the keys whose values you want to fetch from the original array $json_data, like this:

$array_keys = array('second_key', 'another_key');

And second, from your question:

Making a function to which I could pass the $array to a function that has the $key_array set would also be great, like:

echo get_array_value( $array, $array_keys );

Yes, this is certainly possible. The solution would be:

  • Write a function get_array_value() and pass the original array $json_data and the array of keys $array_keys to this function.
  • Create a foreach loop inside get_array_value() function that will loop through the $array_keys array. In each iteration of the loop, call another recursive utility function get_array_value_utility().
  • The get_array_value_utility() function will recursively loop through the array and get the required value from the array.

Here's the code:

function get_array_value_utility($array, $key){
    foreach($array as $k => $v){
        if($key == $k){
            return $v;
        }else if(is_array($v)){
            $v = get_array_value_utility($v, $key);
            if ($v != null) return $v; 
        }
    }
}

function get_array_value($array, $array_keys){
    $values = array();
    foreach($array_keys as $key){
        $values[] = get_array_value_utility($array, $key);
    }
    return $values;
}

$array_keys = array('second_key', 'another_key');
$values = get_array_value($json_data, $array_keys);

// display $values array
var_dump($values);  

Here's the live demo (taken from @Evanrose's comment)

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

8 Comments

is this solution is flexible for any level of nesting
@EaBangalore Yes, this solution is flexible to any level of nesting.
sorry i'm not the owner(questionare)
@EaBangalore Ah, looks like I need some more coffee. ;-)
This works great except that the recursive function stops after it gets to the first key that has an array as its value but don't know why. Here's the live demo. Any ideas?
|

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.