On Thu, Mar 18, 2004 at 02:30:56PM -0500, David T-G wrote:
> Hi, all --
> 
> I have an array like
> 
>   $a =
>     array
>     (
>       'key' =>
>         array
>         (
>           'title' => "Topic Title",
>           'content' => "Topic Content",
>         ),
>         ...
>     ) ;
> 
> and I'd like to sort the whole thing not on the keys but on the titles.
> It sounds like array_multisort should do exactly what I want, but I can't
> seem to get it to work.  For a given source array kind of like
> 
>   $a
>     bookmark
>       How To Bookmark
>       This is all about bookmarking.
>     add
>       Adding Pictures
>       Let's talk about pictures.
>     zebra
>       Caught You Here
>       This is about zebras but it has a surprise title.
> 
> then I want
> 
>   $a
>     add
>       Adding Pictures
>       Let's talk about pictures.
>     zebra
>       Caught You Here
>       This is about zebras but it has a surprise title.
>     bookmark
>       How To Bookmark
>       This is all about bookmarking.
> 
> after sorting.
> 
> Will array_multisort sort on an arbitrary key of a multidimensional
> array, or does it just sort multidimensional arrays but only in key
> index priority?
> 
> Barring that, I imagine I'd have to either ensure that the array is
> constructed in the proper order or make an index of titles => keys so
> that I could sort that and then pull the key out to display the list.
> Bleah.  Once again it would probably be easier to just lay out a DB
> schema!

it looks like array_multisort() will sort one array based on
a sort of another... so you could do something like

    // create an array of titles that can be sorted
    foreach ($a as $k => $v)
        $titles[$k] = $v['title'];

    // sort $titles, then sort $a like $titles
    array_multisort($titles, $a);

- rob

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to