3

EDITED FROM ORIGINAL THAT HAD IMPLIED ONLY 1 ACCESS

If I have an array that contains x number of arrays, each of the form

array('country' => array('city' => array('postcode' => value)))

and another array that might be

array('country', 'city', 'postcode') or array('country', 'city') 

depending on what I need to retrieve, how do I use the second array to identify the index levels into the first array and then access it.

5
  • Is there any particular reason why not doing a simple loop? Commented Dec 17, 2014 at 18:35
  • 1
    I would almost call that a: stealth edit! Please mark it as an EDIT if you extend your question! And if the edit is a completely new question make a new on! Commented Dec 17, 2014 at 18:57
  • It's not a completely new question. It was edited to show that it has to be done multiple times. Commented Dec 17, 2014 at 18:58
  • sorry but this edit is the same of your original question, you can't have more than 1 value inside $array['capital']['city']['postcode'] as I have explained in comments Commented Dec 17, 2014 at 18:59
  • No, you can't. But that wasn't the point. I was simply saying that if the array contains 20 countries, do I need to iterate again and again based on the indexes for each of the 20, or is there a way to assemble the indexes into a variable variable and access each of the 20 using it and not iterating. Commented Dec 17, 2014 at 19:01

3 Answers 3

1

By nesting references with $cur = &$cur[$v]; you can read and modify the original value:
Live example on ide1: http://ideone.com/xtmrr8

$array = array('x' => array('y' => array('z' => 20)));
$keys = array('x', 'y', 'z');

// Start nesting new keys
$cur = &$array;
foreach($keys as $v){
    $cur = &$cur[$v];
}

echo $cur;  // prints 20
$cur = 30;  // modify $array['x']['y']['z']
Sign up to request clarification or add additional context in comments.

6 Comments

I think that's the best possible answer! Very nice explained and good comments! Deserves an upvotes :D
Let me add one more consideration. If I have 20 of $array, say each being a country, and given $keys, I need to access each of the 20 in the same way, is there a way to do it without iterating each?
I don't know if that is a different question or is it related. Anyway if this answer helped you consider to +1 it
Same question. Just trying to consider efficiency. If my keys are ('capital','city','postcode']) and I want the postcode in each of 20 countries, once I determine the keys, is iterating as such what I should do? Could be, that there is no way to do it as a variable variable (like ${$myarray}{['x']['y']['z']} or something to that effect. Just making sure.
how can you have 20 countries if you are accesing an exact item in your array $array['capital']['city']['postcode'] ? Anyway I think this is another question open a new question
|
1

Loop through the array of indexes, traveling down the initial array step by step until you reach the end of the index array.

$array1 = array('x' => array('y' => array('z' => 20)));
$keys = array('x', 'y', 'z');
$array_data = &$array1;
foreach($keys as $key){
    $array_data = &$array_data[$key];
}
echo $array_data;

2 Comments

this will create a new copy of that value
Edited to pass by reference
0

Ok, I apologize for not having expressed my question more clearly, originally, but I got it to work as I was hoping, with a variable variable, as follows:

$keystring = '';
foreach ($keys as $key) {
  $keystring .= "['$key']";
}

Then iterate the main array, for each of the country entries in it, and access the desired value as:

eval('$value = $entry' . "$keystring;");

Comments

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.