Hi. I know sort($array) will sort a 1-D array. However, is there a built-in function like that to sort multidimensional array by one of the dimensions?
for example
$myarray = array();
$myarray['fruits'] = ['apple','orange','banana'];
$myarray['expireDate'] = ['jan','march','february'];
I want to sort fruits and expireDate based on expireDate.. so this pseudo function would return:
somespecialsortfunction($myarray,'expireDate')
$myarray['fruits'] = ['apple','banana','orange'];
$myarray['expireDate'] = ['jan','february','march'];
I realize the dates are just month strings but imagine I have actual dates inside the 'expireDate' array ....
I hope I've made this clear enough...
Thanks!
SS
sort 2-D array or Multi-Dimensional Array
Moderator: General Moderators
-
scienceskillz
- Forum Newbie
- Posts: 3
- Joined: Thu Dec 10, 2009 10:21 pm
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
Re: sort 2-D array or Multi-Dimensional Array
i think you are approaching this wrong. why would you put fruits and their expiration dates in separate arrays? that eliminates the relationship
it should be easier to sort that way but i don't care enough to write code to do it right now. sorry.
Code: Select all
$fruits = array(array('apple', 'jan'), array('orange', 'mar'), array('banana', 'feb'));
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: sort 2-D array or Multi-Dimensional Array
array_multisort()scienceskillz wrote:is there a built-in function like that to sort multidimensional array by one of the dimensions?