mainstreetop Posted July 13, 2010 Share Posted July 13, 2010 Someone from this forum recently helped me with structuring an array. It outputs as... Array ( [525] => Array ( [binders] => 992 ) [372] => Array ( [Corporate Kits] => 105 ) [521] => Array ( [binder Pockets] => 10 ) [431] => Array ( [Desktop Supplies Organizers] => 2 ) [522] => Array ( [binder Posts] => 3 ) ) [brand_id] => Array ( [category] => item_count ) How would I sort the array by category and preserve the keys? I've tried array_multisort(), but can't seem to figure out how to refer to the ['category'] key. Thanks Mike Link to comment https://forums.phpfreaks.com/topic/207549-sorting-a-multidimensional-array/ Share on other sites More sharing options...
khr2003 Posted July 13, 2010 Share Posted July 13, 2010 array_multisort() will reindex numerical associative arrays. Are you trying to assort the first level array or the whole array including all levels down? check out the array sorting functions comparison on this link: http://www.php.net/manual/en/array.sorting.php and see the one the fits your script. Link to comment https://forums.phpfreaks.com/topic/207549-sorting-a-multidimensional-array/#findComment-1085252 Share on other sites More sharing options...
mainstreetop Posted July 13, 2010 Author Share Posted July 13, 2010 KHR2003 - Thank you for responding. I am trying to sort the array all the way down. I wound up rearranging the array as below and then using ksort(). However, I'm sure this is not the most efficient way to accomplish the task. foreach($levels as $category => $value) { foreach($value as $id => $count) { $new[$level] = array($key => $count); } } ksort($new); Link to comment https://forums.phpfreaks.com/topic/207549-sorting-a-multidimensional-array/#findComment-1085274 Share on other sites More sharing options...
Barand Posted July 13, 2010 Share Posted July 13, 2010 I'd redesign the array. Why over complicate by introducing a second level array to store a single element? brand = array ('category', itemcount). A simple asort(myarray) shoulld then suffice as it will sort on the first element by default. Link to comment https://forums.phpfreaks.com/topic/207549-sorting-a-multidimensional-array/#findComment-1085303 Share on other sites More sharing options...
mainstreetop Posted July 13, 2010 Author Share Posted July 13, 2010 Barand - You're right... this does seem overly-complicated. I guess this all started because I need three pieces of data... id, category and count. I need the category id because it is the only unique field. Category can appear more than once in the DB. I used the array approach to group the id's and get a count. It seemed running another mysql query was too taxing. Plus, being new to PHP, I felt this would help understand arrays a little better. I imagine this does not paint a full picture without seeing the code, but I wanted to give some insight as to why I traveled down this path. Mike Link to comment https://forums.phpfreaks.com/topic/207549-sorting-a-multidimensional-array/#findComment-1085306 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.