How to sort a multi-dimension array by value in PHP

In this article we will see how to sort a multi-dimension array by value of one of the keys of the array. We can use a few different methods to do this. One way to to use usort() function. Another way is to just identify the values and create another array with the values and then use it in the array_multisort() function. Using the multisort method we can easily sort a multi-dimension array based on its one or more values. Lets see how we can use both these methods.

You can view the demo of all these methods here.

Sort using usort

The first method to sort the array is by using the usort() function. Here is the code that we can use to perform this sort:

function cmp($a, $b)
{
	return strcmp($a["name"], $b["name"]);
}
usort($vc_array, "cmp");

View Output

The usort() function sorts the $vc_array by using the comparison function (cmp) that we created. The above code can be used to sort the multi-dimension array based on the value of the name column of the array. This is an easy way to sort the multi-dimension array based on the value of one keys.

If you want to sort the array based on values of multiple keys then, you might have to write some complex logic in the callback function to do that. However, there is an alternative way by using the array_multisort() function. The array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions.

Sort using array_multisort by value of 1 key

Lets now see how to use the array_multisort() function to do the same sorting as the one we did using usort above.

foreach ($vc_array as $key => $row)
{
	$vc_array_name[$key] = $row['name'];
}
array_multisort($vc_array_name, SORT_ASC, $vc_array);

View Output

In the above code we first create a new array $vc_array_name to store all the values that we want to sort the $vc_array by. Then we use the array_multisort() to sort the arrays. In this function we first sort the $vc_array_name ascending and then sort the $vc_array.

Sort using array_multisort by value of 2 keys

Now, lets see how we can sort the same array by values of 2 keys of the array. In this example, we will sort by value ascending, name descending.

foreach ($vc_array as $key => $row)
{
	$vc_array_value[$key] = $row['value'];
	$vc_array_name[$key] = $row['name'];
}
array_multisort($vc_array_value, SORT_ASC, $vc_array_name, SORT_DESC, $vc_array);

View Output

This code first sorts the $vc_array_values ascending, $vc_array_name descending and then the $vc_array using both those sorted arrays. Using this method we can sort the multi-dimension array depending on values of multiple different keys.

Sort using array_multisort by value of 3 keys

Now, lets see how we can sort the same array by values of 3 keys of the array. In this example, we will sort by value descending, order descending and name ascending.

foreach ($vc_array as $key => $row)
{
	$vc_array_value[$key] = $row['value'];
	$vc_array_name[$key] = $row['name'];
	$vc_array_order[$key] = $row['order'];
}
array_multisort($vc_array_value, SORT_DESC, $vc_array_order, SORT_DESC, $vc_array_name, SORT_ASC, $vc_array);

View Output

This code first sorts the $vc_array_values descending, $vc_array_order descending, $vc_array_name ascending and then the $vc_array using these sorted arrays. Using this method we can sort the multi-dimension array depending on values of multiple different keys. This method can be extended to sort an array by any number of values.

View All Demo

Related posts:

  1. How to apply a function to every array element in PHP
  2. MySQL ordering results by specific field values
  3. Getting real client IP address in PHP
  4. PHP isset() vs empty() vs is_null()

Leave a Reply