In this solution, we will learn:
solution 1.
$array = array(3, 5, 2, 8);
- How do I sort an array in PHP?
- How do I sort a complex array in PHP?
- How do I sort an array of objects in PHP?
sorting functions
Multi-dimensional arrays, including arrays of objects
$array = array(
array('foo' => 'bar', 'baz' => 42),
array('foo' => ..., 'baz' => ...),
...
);
function cmp(array $a, array $b) {
if ($a['foo'] < $b['foo']) {
return -1;
} else if ($a['foo'] > $b['foo']) {
return 1;
} else {
return 0;
}
}
usort($array, 'cmp');