You are here: Re: Sort array for key « PHP Programming Language « IT news, forums, messages
Re: Sort array for key

Posted by Rik Wasmus on 02/01/08 17:40

On Fri, 01 Feb 2008 18:10:53 +0100, Álvaro G. <Vicario"
<webmasterNOSPAMTHANKS@demogracia.com>> wrote:

> *** Kovu escribió/wrote (Fri, 1 Feb 2008 04:40:45 -0800 (PST)):
>> Hi, I have the next problem. I want to sort the array for the key,

>> using a function. I have been looking for a function to operate with
>> arrays, but I haven't found anything.
>>
>> Ej:
>>
>> Array
>> (
>> [43] => 001480-----3-----04/11/2003
>> [50] => 001000-----3-----04/11/2005
>> )
>>
>>
>> I want to convert this array to:
>>
>> Array
>> (
>> [1] => 001480-----3-----04/11/2003
>> [2] => 001480-----3-----04/11/2003
>> )

> According to your example you want to sort an array discarding keys.

Not really, we have 2 different values here, I guess it's just a
preview/part of an array. Array index 50 would come before index 43 BTW,
001000 before 001480 offcourse trumps a later date. Or it's a typo. In
either case, difficult to interpret with 100% certainty. I'd advise the up
to look at the different functions able to sort arrays in
<http://nl2.php.net/manual/en/ref.array.php>

Offcourse, this dubious example could mean the OP wants to sort by the
ending date rather then the string value. Which would be something like:

function __mysort($a,$b){
$a_cmp = preg_match('%\d+/d+/\d+^%',$a,$a_match) ?
strtotime($a_match[0]):0;
$b_cmp = preg_match('%\d+/d+/\d+^%',$b,$b_match) ?
strtotime($b_match[0]):0;
if($a_cmp == $b_cmp) return 0;
return ($a_cmp < $b_cmp) ? -1 : 1;
}
usort($array,'__mysort');
--
Rik Wasmus

 

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

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