How to sort a multidimensional PHP array by value

We can use the usort() function with a user-defined comparison function to sort a multidimensional PHP array by value. With this function, we can design our own unique comparison logic and have more control over the sorting process. The usort() function sorts the multidimensional array in place. It modifies the original array, rearranging its elements based on the comparison function we provide.

Syntax

Here is the basic syntax of the usort() function:

usort(array, callback)

The array parameter specifies the array to sort. The callback parameter represents the comparison function and returns an integer that indicates the relationship between the two elements of the array. For example, if the first element is <<, ==, or >> than the second element, then the function must return an integer based on the specific condition.

Code example

In the code example, we implement a comparison function for usort() that sorts a multidimensional PHP array by value in ascending order.

<?php
$multiDimensionalArray = array(
array('product' => 'Fries', 'price' => 500),
array('product' => 'Pizza', 'price' => 2200),
array('product' => 'Burger', 'price' => 1360),
array('product' => 'Drink', 'price' => 300),
);
function compareByPrice($x, $y) {
if ($x['price'] == $y['price']) {
return 0;
}
return ($x['price'] < $y['price']) ? -1 : 1;
}
// Sort the array by age using the compareByPrice function
usort($multiDimensionalArray, 'compareByPrice');
// Print the sorted array
foreach ($multiDimensionalArray as $product) {
echo 'Product: ' . $product['product'] . ', Price: ' . $product['price'] . "\n";
}
?>

Code explanation

  • Lines 2–7: We declare a variable that holds a multidimensional array. Each element of the array represents a product and contains two key-value pairs: product and price.

  • Lines 9–14: We define a custom comparison function named compareByPrice, which takes the $x and $y arguments. In this case, it compares two elements (products) in the array based on their prices.

    • if ($x['price'] == $y['price']): This condition checks if the prices of two products are equal.

    • return 0: If the prices are equal, this function returns 0.

    • return ($x['price'] < $y['price']) ? -1 : 1: If the prices are not equal, this function returns -1 if the price of $x is less than the price of $y, or 1 if the price of $x is greater than the price of $y. This function determines the order in which the elements should be sorted in the array.

  • Line 17: We use the usort($multiDimensionalArray, 'compareByPrice') function to sort the $multiDimensionalArray based on the custom comparison function. This function takes the array to be sorted ($multiDimensionalArray) and the name of the custom comparison function (compareByPrice) as arguments.

  • Lines 20–22: We start a foreach loop that iterates through each element (product) in the sorted array.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved