Newsgroup: it.comp.www.php
Titolo: Doppio ordinamento array?

Ciao
č possibile ordinare un array prima per valore e poi per chiave?
o meglio ordinare per valore e mostrare i risultati ordinati per valore, 
ordinati per chiave?
ovvero se ho
pippo 1
antonio 2
rocco 2
fabio 2
gianni 3
dovrei avere
1 pippo
2 antonio
2 fabio
2 rocco
3 gianni
ho provato con
ksort(arsort($array) e viceversa
ma niente
c'č un modo di farlo senza fare piů cicli???

lurkettone wrote:

Ovvero hai delle chiavi doppie (sarebbe un errore)  o un array 
[bi|multi]dimensionale.
Nel secondo  caso personalemente consiglio di uilizzare questa funzione 
(copio/incollo, al solito da 
http://it.php.net/manual/it/function.array-multisort.php
):
Ciao
8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----
ok, now i made it a real (prooved) function, cause i needed to sort by 
two of the columns and it didn't work, sorting twice...
is was kinda tricky, cause i never handled with eval in php, but now 
it's quite superb - ok i didn't any errorhandling, but who cares *g*
___________
the function:
function array_csort() {  //coded by Ichier2003
    $args = func_get_args();
    $marray = array_shift($args);
    $msortline = "return(array_multisort(";
    foreach ($args as $arg) {
        $i++;
        if (is_string($arg)) {
            foreach ($marray as $row) {
                $sortarr[$i][] = $row[$arg];
            }
        } else {
            $sortarr[$i] = $arg;
        }
        $msortline .= "\$sortarr[".$i."],";
    }
    $msortline .= "\$marray));";
    eval($msortline);
    return $marray;
}
the fine thing is, you can add as many parameters you need, and you can 
add sort_flags as well (but be shure not to put em into " or ' - took 
some time for me to get this *g*)
______
syntax:
$new_array = array_multisort($array [, 'col1' [, SORT_FLAG [, 
SORT_FLAG]]]...);
__________
explanation:
$array is the array you want to sort, 'col1' is the name of the column 
you want to sort, SORT_FLAGS are : SORT_ASC, SORT_DESC, SORT_REGULAR, 
SORT_NUMERIC, SORT_STRING
you can repeat the 'col',FLAG,FLAG, as often you want, the highest 
prioritiy is given to the first - so the array is sorted by the last 
given column first, then the one before ...
_______
example:
in my case the array has it's index as first dimension (i like that for 
using foreach later):
$array[0]['name'] = "Niko";
$array[0]['age'] = 24;
$array[0]['town'] = "Berlin";
$array[1]['name'] = "Dennis";
$array[1]['age'] = 34;
$array[1]['town'] = "Berlin";
$array[2]['name'] = "Oliver";
$array[2]['age'] = 44;
$array[2]['town'] = "Aachen";
$array[3]['name'] = "Andi";
$array[3]['age'] = 34;
$array[3]['town'] = "Berlin";
_____________________
use the function like this:
$array = array_csort($array,'town','age',SORT_DESC,'name');
here the resulting array would be:
$array[0]['name'] = "Oliver";
$array[0]['age'] = 44;
$array[0]['town'] = "Aachen";
$array[1]['name'] = "Andi";
$array[1]['age'] = 34;
$array[1]['town'] = "Berlin";
$array[2]['name'] = "Dennis";
$array[2]['age'] = 34;
$array[2]['town'] = "Berlin";
$array[3]['name'] = "Niko";
$array[3]['age'] = 24;
$array[3]['town'] = "Berlin";
that should be enough :)
.. waiting this fuc to be included to the next php-release *gg*
hope it helps you..Niko