I'm trying to sort an array by a specific property (weight), I found some possible solutions but they did not work for me, could anyone tell me where I'm doing something wrong or another way out of my need?
How am I trying to sort:
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 />";
}
Output:
teste5.php - 9
teste4.php - 4
teste3.php - 8
teste2.php - 5
teste1.php - 10