kenoli Posted February 11, 2012 Share Posted February 11, 2012 I have a multi-dimensional array that looks like this: Array( [1] => Array ( [vid] => 1 [media] => Giclee => 12"x12" [price] => 100.00 ) [2] => Array ( [vid] => 2 [media] => Silk on Wood => 12"x12" [price] => 75.00 ) [3] => Array ( [vid] => 3 [media] => Giclee => 24"x24" [price] => 4000.00 ) ) I want to sort it so that the top level array items (the ones with numeric keys) are ordered alphabetically on the content of the nested item with the key "media." I have looked at the function array_multisort but can't figure out how to set it up to do what I want. Can anyone help me? Thanks, --Kenoli Link to comment Share on other sites More sharing options...
justsomeguy Posted February 12, 2012 Share Posted February 12, 2012 I would use usort: function sort_by_media($a, ${ return strcmp($a['media'], $b['media']);} usort($data, 'sort_by_media'); http://www.php.net/m...ction.usort.php Link to comment Share on other sites More sharing options...
kenoli Posted February 13, 2012 Author Share Posted February 13, 2012 Thanks. I will take a look at this. I also need to re-think the database sort angle. There was some reason I didn't think this would work, but I'm going to have to take a moment to look at it. I've also been refactoring this code and need to see where things are now. I appreciate your taking the time to respond. Your response is useful. --Kenoli Link to comment Share on other sites More sharing options...
kenoli Posted March 1, 2012 Author Share Posted March 1, 2012 Wanted to close this query with a final thanks. I realized that the simple solution here for me was a join that allowed me to sort on the desired field when I retrieved the objects. --Kenoli Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.