multidimensional array and storing locations of values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cone13cone
Forum Newbie
Posts: 24
Joined: Fri Mar 20, 2009 8:32 pm

multidimensional array and storing locations of values

Post by cone13cone »

I have an object which creates a multidimensional array. I would like to be able to store the locations of the values in a separate array. Kinda like a road map of the original array, so when I update via JSON my script will know where to look for the specified value inside of the updated array.

$array = ['first'][1]['second'][0]['item'] = 'some item to be updated';

What I need to store is the ['first'][1]['second'][0]['item'] which is the location of 'some value to be updated';

Ideally after my array is completed I would like to be able to loop through it and store the location of each value like above.

Any ideas or suggestions would be appreciated.

Edit: also what would be the best way to store the location, Im assuming a string.
limitdesigns
Forum Commoner
Posts: 25
Joined: Sat Feb 06, 2010 9:05 pm

Re: multidimensional array and storing locations of values

Post by limitdesigns »

You can do a series of foreach loops that parse through each of your arrays within the main array.

Example:

Code: Select all

 
$array = array(your_array);
// First dimension
foreach($array as $key=>$value) {
     // Second dimension
     foreach($value as $key2=>$value2) {
          do_something();
     }
}
 
Post Reply