You are here: Re: array_multisort() « PHP Programming Language « IT news, forums, messages
Re: array_multisort()

Posted by Schraalhans Keukenmeester on 03/28/07 08:35

ZeldorBlat wrote:
> On Mar 27, 5:20 pm, Schraalhans Keukenmeester <bitbuc...@invalid.spam>
> wrote:
>> I'm having a hard time getting to grips with this function.
>>
>> I have an array shaped like this:
>>
>> $dircontents[0]['filename'] ='index.html'
>> $dircontents[0]['owner'] = 'www'
>> $dircontents[0]['group'] = 'www'
>> $dircontents[0]['type'] = 'file'
>>
>> $dircontents[1]['filename'] ='images'
>> $dircontents[1]['owner'] = 'www'
>> $dircontents[1]['group'] = 'www'
>> $dircontents[1]['type'] = 'dir'
>>
>> etc.
>>
>> 'type' can be any of {dir,file,link}
>>
>> How do I get the array sorted, alphabetically, first by type, then filename?
>> My attempts sofar have lead to a seemingly unsorted array, not even
>> close to anything recognizable. Is array_multisort() even what I should
>> be looking for?
>>
>> Help appreciated!
>> Sh.
>
> Try usort() instead:
>
> function cmp($a, $b) {
> if($a['type'] < $b['type'])
> return -1;
> elseif($a['type'] > $b['type'])
> return 1;
> elseif($a['filename'] < $b['filename'])
> return -1;
> elseif($a['filename'] > $b['filename'])
> return 1;
> else
> return 0;
> }
>
> usort($dircontents, 'cmp');
>
Great! That's what I needed indeed. Somewhere in the back of my head...
Thanx Zeldor!

 

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

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