How can I sort arrays and data in PHP?

Convert number to month name in PHP
calculate math expression from a string using eval

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');


Convert number to month name in PHP
calculate math expression from a string using eval
en English
X
Scroll to Top