How do you sort an array by a specific value in php?

I have an array with specific values in it and i would like to sort the array on a specific value in it. For instance, TOTCOM_METIER DESC. Ex :

Array
(
[0] => Array
    (
        [TOTCOM_METIER] => 1
        [metier] => Traiteur
    )

[1] => Array
    (
        [TOTCOM_METIER] => 4
        [metier] => Restauration traditionnelle
    )

[2] => Array
    (
        [TOTCOM_METIER] => 2
        [metier] => Coiffure
    )

)

I would like to sort it on TOTCOM_METIER DESC to have this result :

Array
(
[0] => Array
    (
        [TOTCOM_METIER] => 4
        [metier] => Restauration traditionnelle
    )

[1] => Array
    (
        [TOTCOM_METIER] => 2
        [metier] => Coiffure
    )

[2] => Array
    (
        [TOTCOM_METIER] => 1
        [metier] => Traiteur
    )

)

asked Mar 20, 2017 at 9:01

0

 Array
        (
            'TOTCOM_METIER' => 1,
            'metier' => 'Traiteur'
        ),
    1 => Array
        (
            'TOTCOM_METIER' => 4,
            'metier' => 'Restauration traditionnelle'
        ),
    2 => Array
        (
            'TOTCOM_METIER' => 2,
            'metier' => 'Coiffure'
        )
);

//define custom "comparator" (in reverse order)
function cmp($a, $b){
    $key = 'TOTCOM_METIER';
    if($a[$key] < $b[$key]){
        return 1;
    }else if($a[$key] > $b[$key]){
        return -1;
    }
    return 0;
}
usort($arr, "cmp");

print_r($arr);

?>

answered Mar 20, 2017 at 9:07

ewczewcz

12.2k1 gold badge23 silver badges45 bronze badges

2

Try this,

function compare($a, $b)
{
   return ($a['TOTCOM_METIER']< $b['TOTCOM_METIER']);
}
usort($your_array, "compare");

Here is usort docs which states Sort an array by values using a user-defined comparison function

answered Mar 20, 2017 at 9:05

RahulRahul

17.9k7 gold badges40 silver badges58 bronze badges

How do you get this data ? If you select from db system,you can use order by TOTCOM_METIER desc in your select sql.

answered Mar 20, 2017 at 9:06

With this you can pass the order you want, "desc" or "asc"

function sortAsc($a, $b) {
   return ($a[TOTCOM_METIER] < $b[TOTCOM_METIER]);
}

function sortDesc($a, $b) {
   return ($a[TOTCOM_METIER] > $b[TOTCOM_METIER]);
}

function sortMyArray($array, $order = "asc") {
    if($order === "desc") {
        return usort($array, "sortDesc");
    }

    return usort($array, "sortAsc");
}

// Call it like
sortMyArray($array, "asc");

answered Mar 20, 2017 at 9:13

napoluxnapolux

14.8k9 gold badges50 silver badges70 bronze badges


The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.


PHP - Sort Functions For Arrays

In this chapter, we will go through the following PHP array sort functions:

  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order, according to the value
  • ksort() - sort associative arrays in ascending order, according to the key
  • arsort() - sort associative arrays in descending order, according to the value
  • krsort() - sort associative arrays in descending order, according to the key

Sort Array in Ascending Order - sort()

The following example sorts the elements of the $cars array in ascending alphabetical order:

Example

$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>

Try it Yourself »

The following example sorts the elements of the $numbers array in ascending numerical order:



Sort Array in Descending Order - rsort()

The following example sorts the elements of the $cars array in descending alphabetical order:

Example

$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>

Try it Yourself »

The following example sorts the elements of the $numbers array in descending numerical order:


Sort Array (Ascending Order), According to Value - asort()

The following example sorts an associative array in ascending order, according to the value:

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>

Try it Yourself »


Sort Array (Ascending Order), According to Key - ksort()

The following example sorts an associative array in ascending order, according to the key:

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>

Try it Yourself »


Sort Array (Descending Order), According to Value - arsort()

The following example sorts an associative array in descending order, according to the value:

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
arsort($age);
?>

Try it Yourself »


Sort Array (Descending Order), According to Key - krsort()

The following example sorts an associative array in descending order, according to the key:

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>

Try it Yourself »


Complete PHP Array Reference

For a complete reference of all array functions, go to our complete PHP Array Reference.

The reference contains a brief description, and examples of use, for each function!


PHP Exercises



How do you sort a value in an array?

PHP - Sort Functions For Arrays sort() - sort arrays in ascending order. rsort() - sort arrays in descending order. asort() - sort associative arrays in ascending order, according to the value. ksort() - sort associative arrays in ascending order, according to the key.

How do you sort an array in a specific order?

Sorting an array of objects in javascript is simple enough using the default sort() function for all arrays: const arr = [ { name: "Nina" }, { name: "Andre" }, { name: "Graham" } ]; const sortedArr = arr.

How can we sort an array without using sort method in PHP?

php function sortArray() { $inputArray = array(8, 2, 7, 4, 5); $outArray = array(); for($x=1; $x<=100; $x++) { if (in_array($x, $inputArray)) { array_push($outArray, $x); } } return $outArray; } $sortArray = sortArray(); foreach ($sortArray as $value) { echo $value . "
"; } ?>

How do I sort a key in PHP?

The ksort() function sorts an associative array in ascending order, according to the key. Tip: Use the krsort() function to sort an associative array in descending order, according to the key. Tip: Use the asort() function to sort an associative array in ascending order, according to the value.