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 : Show
I would like to sort it on TOTCOM_METIER DESC to have this result :
asked Mar 20, 2017 at 9:01
0
answered Mar 20, 2017 at 9:07
ewczewcz 12.2k1 gold badge23 silver badges45 bronze badges 2 Try this,
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 answered Mar 20, 2017 at 9:06
With this you can pass the order you want, "desc" or "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 ArraysIn this chapter, we will go through the following PHP array sort functions:
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"); 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"); 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"); 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"); 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"); 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"); Try it Yourself » Complete PHP Array ReferenceFor 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 ExercisesHow 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.
|