Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How to sort associative arrays in descending order, according to the value in php ?

user-image
Question ajoutée par mohamed ahmed mahmoud , مصمم مواقع انترنت web designer , conect
Date de publication: 2017/04/02
medhatmahmoud mahmoud
par medhatmahmoud mahmoud , Web Designer , Connect Computer Service Co

<?php

$age= array("Peter"=>"", "Ben"=>"", "Joe"=>"");

arsort($age);

 

?>

Mohamed Ali
par Mohamed Ali , Senior Online Developer , Al Arab News Channel

Using usort + anonymous function

http://php.net/manual/en/function.usort.php

http://php.net/manual/en/functions.anonymous.php

 

You can do an ascending sort like this:

usort($inventory, function ($item1, $item2) {

    return $item1['price'] <=> $item2['price'];

});

 

Or a descending sort like this:

usort($inventory, function ($item1, $item2) {

    return $item2['price'] <=> $item1['price'];

});

 

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?