Sort array for key
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
)
Thaks.
Re: Sort array for key
On 1 Feb, 12:40, Kovu <kovut... [at] gmail.com> wrote:
> 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
> )
>
> Thaks.
Wow you must have looked really hard!
Did you even put
php array sort key
into Google?
Re: Sort array for key
Kovu wrote:
> 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
> )
IMO this is not even sorting, but "flushing" some array.
How hard can this be?
function arrayFlush($mixArray){
foreach($mixArray as $obj) $temp[] = $obj;
return $temp;
}
Or am I missing something?
--
Freundliche Grüße,
Franz Marksteiner
Re: Sort array for key
*** 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,
According to your example you want to sort an array discarding keys.
Surprisingly, the function is called "sort()". From manual:
sort - Sort an array available since: PHP 3, PHP 4, PHP 5
usage:
bool sort ( array &array [, int sort_flags] )
Note: This function assigns new keys for the elements in array . It will
remove any existing keys you may have assigned, rather than just reordering
the keys.
> 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
> )
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor austrohúngaro: http://www.demogracia.com
--
Re: Sort array for key
On Fri, 01 Feb 2008 18:10:53 +0100, Álvaro G. <Vicario"
<webmasterNOSPAMTHANKS [at] 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