How to sort a multi-dimension array by value in PHP - Demo
This is a demo page for How to sort a multi-dimension array by value in PHP
In this demo we will sort a sample multi-dimension array by value of one and more keys using different methods. Here is list of different sorts that we will be doing:
- Sort using usort
- Sort using array_multisort by value of 1 key
- Sort using array_multisort by value of 2 keys
- Sort using array_multisort by value of 3 keys
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,
),
)