简体   繁体   中英

Use A Variable for an Array of Keys in Multidimentional Arrays in PHP

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.

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM