8
I am trying to sort an array by a specific property(weight), I found some possible solutions but they did not work for my case, someone would know where I am doing something wrong or another way out of my need?
How I’m trying to accomplish ordination:
function ordenaPorPeso($a, $b) {
if($a->peso == $b->peso){ return 0 ; }
return ($a->peso < $b->peso) ? -1 : 1;
}
$arrayPaginas = array(
array("url" => "teste1.php", "peso" => 10),
array("url" => "teste2.php", "peso" => 5),
array("url" => "teste3.php", "peso" => 8),
array("url" => "teste4.php", "peso" => 4),
array("url" => "teste5.php", "peso" => 9)
);
usort($arrayPaginas, 'ordenaPorPeso');
foreach($arrayPaginas as $pagina){
echo $pagina["url"]. " - " . $pagina["peso"] . "<br /><br />";
}
Exit:
teste5.php - 9
teste4.php - 4
teste3.php - 8
teste2.php - 5
teste1.php - 10
The correct is
$a['peso']
and$b['peso']
– Rodrigo Siqueira
I think this should help you Array Multisort Exemple
– Elton Schivei Costa