mjahkoh Posted April 21, 2012 Share Posted April 21, 2012 <?php $menu = array( '22' => array ( 'title'=>'Businesses' ,'busstype'=>array( 5=>'Printers' ,2=>'Bookshops' ,6=>'Publishers' ,8=>'Uniform Distributors' ) ) ,21 => array ( 'title'=>'Academic' ,'busstype'=>array ( 5=>'Academy' ,6=>'Primary' ,7=>'Polytechnic' ,11=>'University' ) ) ,'Events' => array ( 'title'=>'Events' ,'busstype'=>array ( 3=>'Music' ,4=>'Road shows' ,6=>'Sports' ,6=>'Yoga' ) ) ) ; I have a dynamically created menu as above. How would i sort on the 'busstype' homogenously and print it while maintaing the key. Results should be like <a href="search.php?listing=21&busstype=5">Academy</a> <a href="search.php?listing=22&busstype=2">Bookshops</a> ....... <a href="search.php?listing=Events&busstype=6">Yoga</a> ?> Link to comment https://forums.phpfreaks.com/topic/261364-sorting-then-printing-multidimension-array/ Share on other sites More sharing options...
kicken Posted April 21, 2012 Share Posted April 21, 2012 Create a function that contains the logic for the sorting, then use uksort with that function to perform the sort. Your function should accept two arguments, $item1 and $item2 and return a value as follows: [*] Return a value less than zero if $item1 sorts before $item2 [*] Return zero if $item1 is the same as $item2 [*] Return a value greater than zero if $item2 sorts before $item1 Then just use a normal foreach loop to output the array after it's sorted. Link to comment https://forums.phpfreaks.com/topic/261364-sorting-then-printing-multidimension-array/#findComment-1339293 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.