Jump to content

Sorting then printing multidimension array


mjahkoh

Recommended Posts

<?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>

 

?>

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.