Monday 16 July 2018

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

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


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:
1
2
3
4
5
function cmp($a, $b)
{
    return strcmp($a["name"], $b["name"]);
}
usort($vc_array, "cmp");
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.
1
2
3
4
5
foreach ($vc_array as $key => $row)
{
    $vc_array_name[$key] = $row['name'];
}
array_multisort($vc_array_name, SORT_ASC, $vc_array);
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.
1
2
3
4
5
6
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);
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.
1
2
3
4
5
6
7
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);

This code first sorts the $vc_array_values descending, $vc_array_order descending, $vc_array_nameascending 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.

Sample array

Here is the array that we will sort in this demo:
array ( 0 => array ( 'id' => 1, 'name' => 'xyz', 'value' => 'abc', 'order' => 6, ), 1 => array ( 'id' => 2, 'name' => 'abc', 'value' => 'xyz', 'order' => 2, ), 2 => array ( 'id' => 3, 'name' => 'uvw', 'value' => 'ghi', 'order' => 1, ), 3 => array ( 'id' => 4, 'name' => 'def', 'value' => 'xyz', 'order' => 2, ), 4 => array ( 'id' => 5, 'name' => 'ghi', 'value' => 'uvw', 'order' => 3, ), 5 => array ( 'id' => 6, 'name' => 'ghi', 'value' => 'def', 'order' => 3, ), )

Sort using usort

Now lets sort this array using usort. We will use the following code to do this sort.
function cmp($a, $b) { return strcmp($a["name"], $b["name"]); } usort($vc_array, "cmp");
Here is how the sorted array will be.
array ( 0 => array ( 'id' => 2, 'name' => 'abc', 'value' => 'xyz', 'order' => 2, ), 1 => array ( 'id' => 4, 'name' => 'def', 'value' => 'xyz', 'order' => 2, ), 2 => array ( 'id' => 5, 'name' => 'ghi', 'value' => 'uvw', 'order' => 3, ), 3 => array ( 'id' => 6, 'name' => 'ghi', 'value' => 'def', 'order' => 3, ), 4 => array ( 'id' => 3, 'name' => 'uvw', 'value' => 'ghi', 'order' => 1, ), 5 => array ( 'id' => 1, 'name' => 'xyz', 'value' => 'abc', 'order' => 6, ), )

Sort using array_multisort by value of 1 key

Now lets sort this array using array_multisort. We will sort the array by the values of 'name' key. We will use the following code to do this sort.
foreach ($vc_array as $key => $row) { $vc_array_name[$key] = $row['name']; } array_multisort($vc_array_name, SORT_ASC, $vc_array);
Here is how the sorted array will be.
array ( 0 => array ( 'id' => 2, 'name' => 'abc', 'value' => 'xyz', 'order' => 2, ), 1 => array ( 'id' => 4, 'name' => 'def', 'value' => 'xyz', 'order' => 2, ), 2 => array ( 'id' => 5, 'name' => 'ghi', 'value' => 'uvw', 'order' => 3, ), 3 => array ( 'id' => 6, 'name' => 'ghi', 'value' => 'def', 'order' => 3, ), 4 => array ( 'id' => 3, 'name' => 'uvw', 'value' => 'ghi', 'order' => 1, ), 5 => array ( 'id' => 1, 'name' => 'xyz', 'value' => 'abc', 'order' => 6, ), )

Sort using array_multisort by value of 2 keys

Now lets sort this array using array_multisort. We will sort the array by the values of 'value' in ascending order and 'name' in descending order. We will use the following code to do this sort.
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);
Here is how the sorted array will be.
array ( 0 => array ( 'id' => 1, 'name' => 'xyz', 'value' => 'abc', 'order' => 6, ), 1 => array ( 'id' => 6, 'name' => 'ghi', 'value' => 'def', 'order' => 3, ), 2 => array ( 'id' => 3, 'name' => 'uvw', 'value' => 'ghi', 'order' => 1, ), 3 => array ( 'id' => 5, 'name' => 'ghi', 'value' => 'uvw', 'order' => 3, ), 4 => array ( 'id' => 4, 'name' => 'def', 'value' => 'xyz', 'order' => 2, ), 5 => array ( 'id' => 2, 'name' => 'abc', 'value' => 'xyz', 'order' => 2, ), )

Sort using array_multisort by value of 3 keys

Now we will sort the array by the values of 'value' in descending order, 'order' in descending order and 'name' in ascending order. We will use the following code to do this sort.
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);
Here is how the sorted array will be.
array ( 0 => array ( 'id' => 2, 'name' => 'abc', 'value' => 'xyz', 'order' => 2, ), 1 => array ( 'id' => 4, 'name' => 'def', 'value' => 'xyz', 'order' => 2, ), 2 => array ( 'id' => 5, 'name' => 'ghi', 'value' => 'uvw', 'order' => 3, ), 3 => array ( 'id' => 3, 'name' => 'uvw', 'value' => 'ghi', 'order' => 1, ), 4 => array ( 'id' => 6, 'name' => 'ghi', 'value' => 'def', 'order' => 3, ), 5 => array ( 'id' => 1, 'name' => 'xyz', 'value' => 'abc', 'order' => 6, ), )

0 comments:

Post a Comment