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.