Processing a sorted multi-dimensional array

I have an array of structure $arrname[‘name’][0,1,2,3,4,…], $arrname[‘city’][0,1,2,3,4,…], $arrname[‘state’][0,1,2,3,4,…] and if I do an asort($arrname[‘name’]) it performs the sort just fine by name and maintains the indexing so I can access the other elements by index.

But how do I now process the array since the index is out of order. Normally I would use a FOR statement processing from 0 to count but since the name array is no longer in index order, it wipes out the sort.

So I assume I need to process the name array sequentially and reference the other two elements by index which I would need to get from the name element. Is there a way in PHP to process the array sequentially by ordered name element and reference the index of the sorted name element?


I think I found the answer. If I use array_keys to create an array of the $arrname[‘name’] elements and then process the array_keys result in a FOR statement, it gives me the corresponding array indexes for each name element in sorted order. Seems to work fine.

If you use

foreach ($arrname['name'] as $key => $value)

that should give you the key for each ‘name’ element too.

Excellent, thanks for the input. I do appreciate it. I was starting to look into foreach when I ran across the other solution so this helps make it more clear.