Sort array for key

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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.


    [43] => 001480-----3-----04/11/2003
    [50] => 001000-----3-----04/11/2005

I want to convert this array to:

    [1] => 001480-----3-----04/11/2003
    [2] => 001480-----3-----04/11/2003


Re: Sort array for key

Quoted text here. Click to load it

Wow you must have looked really hard!
Did you even put
php array sort key
into Google?

Re: Sort array for key

Kovu wrote:
Quoted text here. Click to load it

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)):
Quoted text here. Click to load it

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
    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.

Quoted text here. Click to load it

-+ - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web:
+- Mi web de humor austrohúngaro:

Re: Sort array for key

On Fri, 01 Feb 2008 18:10:53 +0100, Álvaro G. <Vicario"  

Quoted text here. Click to load it

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  

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) ?  
    $b_cmp = preg_match('%\d+/d+/\d+^%',$b,$b_match) ?  
    if($a_cmp == $b_cmp) return 0;
    return ($a_cmp < $b_cmp) ? -1 : 1;
Rik Wasmus

Re: Sort array for key

Greetings, Kovu.
In reply to Your message dated Friday, February 1, 2008, 15:40:45,

Quoted text here. Click to load it

If difference in the array values is a mistake and You want to just reindex
array values - try

$new = array_values($old);


Site Timeline