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>

 

?>

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.