Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

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

user-image
Question added by mohamed ahmed mahmoud , مصمم مواقع انترنت web designer , conect
Date Posted: 2017/04/02
medhatmahmoud mahmoud
by medhatmahmoud mahmoud , Web Designer , Connect Computer Service Co

<?php

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

arsort($age);

 

?>

Mohamed Ali
by 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

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.