PHP Array Sorting: Sort by Value with asort()

In PHP, you can use the asort() function to sort an array by its values. The asort() function will sort the array in ascending order based on its values, while maintaining the key-value relationship of the array.

I will go to the store to buy some groceries.

$fruits = array("apple" => 4, "banana" => 2, "cherry" => 3);
asort($fruits);

foreach($fruits as $fruit => $quantity) {
    echo "$fruit: $quantity\n";
}

Output result:

banana: 2
cherry: 3
apple: 4
bannerAds