You are here: Re: Sorting Multidimensional Array? « PHP Language « IT news, forums, messages
Re: Sorting Multidimensional Array?

Posted by benb on 01/30/07 09:30

"Colin McKinnon"
<colin.thisisnotmysurname@ntlworld.deletemeunlessURaBot.com> wrote in
message news:Yotvh.85063$z01.32348@newsfe3-gui.ntli.net...
> benb wrote:
>
>> <snip>
>> $read = ldap_search($connect, $base_dn, $filter, $inforequired)
>> $info = ldap_get_entries($connect, $read);
>>
>> This has returned an array (not sure of the type, is it multidimensional,
>> or an array of arrays?) with 2 values:
>>
>> $info[$i]["displayname"][0]
>> and
>> $info[$i]["mobile"][0]
>>
> <snip>
>>
>> How do I go about sorting this array, so that [displayname] is
>> alphabetical? I've tried using sort($info) but this seems to wipe the
>> array, I've also tried sort($info[0]["displayname"]) but that doesn't
>> seem
>> to sort anything!
>>
>
> You'll need to write your own comparison function (not tested - YMMV):
>
>
> function my_sort(&$a, &$b)
> {
> global $sort_by;
> if ($a[$sort_by][0]==$b[$sort_by][0]) return 0;
> return ($a[$sort_by][0]<$b[$sort_by][0]) ? -1 : 1;
> }
>
> $sort_by = 'displayname';
> usort($info, 'my_sort');
> // or if you prefer...
> $sort_by = 'mobile';
> usort($info, 'my_sort');
>
>
>
> Note that you don't have to worry about iterating through th array
> yourself,
> or having to code your own quicksort when you discover how slow a bubble
> sort really is...etc.
>
> HTH
>
> C.

Hi Colin,

Thanks for the code, I shall give it a try and report back!

Ben

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация