All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

PHP Sort Multidimensional Array By Value Descending

Last Updated : Mar 11, 2024

PHP Sort Multidimensional Array By Value Descending

In this tutorial we will show you the solution of PHP sort multidimensional array by value descending, here we defined multidimensional array with three set of values then using array_column() and array_multisort() methods we sort easily.

The array_column() function returns the values from a single column in the input array.

The array_multisort() function returns a sorted array. We can assign one or more arrays.

The function sorts the first array and the other arrays follow then if two or more values are the same, it sorts the next array and so on.

Step By Step Guide On PHP Sort Multidimensional Array By Value Descending :-

Here we declared our multidimensional array ‘$f’ with three set of array with two columns of values.

Then using array_column() method we retrieves column ‘flimname’ values and stored to variable ‘$name’ and in array_multisort() we need to pass three parameters so we passed column value of variable ‘$name’, type of sorting ‘SORT_DESC’ it refers descending order, finally array $f.

Result of multisort will change the original array into descending order so after multisort() method when we print array $f we gets result of descending ordered array.

array_multisort($name, SORT_DESC,$f);
echo "Multisort Descending Order<br>";
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. For declare an associative array we need to know syntax of array declaration in php (i.e) $array_name=array(array(key1=>value1,key2=>value2), array(key1=>value3,key2=>value4)..).
  4. We declared array $f with three set of array values ‘array("flimname"=>"Multiverse","rating"=>6.7), array("flimname"=>"Thor","rating"=>8.2), array("flimname"=>"Avengers","rating"=>7.2),’.
  5. Using array_column() method we filtered column "flimname" from an associative $f then stored to variable ‘$name’ array for pass column values on array_multisort() method.
  6. We can also use column ‘rating’ in array $f instead of column "flimname".
  7. Lastly using array_multisort method with parameters of ‘column array $name, descending order type SORT_DESC, multidimensional array’ we sorted $f array in descending order.
  8. So the result will contain ‘Array ( [0] => Array([flimname] => Thor [rating] => 8.2 ) [1] => Array ( [flimname] => Multiverse [rating] => 6.7 ) [2] => Array ( [flimname] => Avengers [rating] => 7.2)).
  9. We can create different array with some other values as your wish, here shown example for your reference only try to explore different values but concept will same so it won’t affect the result.

Conclusion :-

In conclusion we are able to know how to sort multidimensional array values in descending order using php.

First we need to start our xampp server then we load this program on browser we can see result of multidimensional array in descending order when we need to display any array with whole values we need to use print_r() method and we can also use this concept except type of sorting SORT_DESC we can get multidimensional array values in ascending order by modifying sort type to ‘SORT_ASC’.

I hope this tutorial on PHP sort multidimensional array by value descending helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪