PHP - Sort Multidimensional Array By Key

In this tutorial we will create a Sort Multidimensional Array By Key using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Sort Multidimensional Array By Key</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form method="POST" action="">
  18. <div class="form-inline">
  19. <select name="sort" required="required" class="form-control">
  20. <option value="SORT_ASC">ASCENDING</option>
  21. <option value="SORT_DESC">DESCENDING</option>
  22. </select>
  23. <button name="toggle" class="btn btn-primary">Sort</button>
  24. <div>
  25. </form>
  26. <br />
  27. <table class="table table-bordered">
  28. <thead>
  29. <tr>
  30. <th>Name</th>
  31. <th>Price</th>
  32. </tr>
  33. </thead>
  34. <tbody>
  35. <?php include 'sort.php'?>
  36. </tbody>
  37. </table>
  38. </div>
  39. </body>
  40. </html>

Creating Main Function

This is where the main function of the application. This code will sort the multidimensional array To do this just copy and write these block of codes inside the text editor, then save it as sort.php.
  1. <?php
  2. $phones = array(
  3. array("name" => "Samsung Galaxy 8", "price" => "P15,990"),
  4. array("name" => "Iphone X", "price" => "P53,300"),
  5. array("name" => "Lenovo Z5", "price" => "P11,771.99"),
  6. array("name" => "Sony Xperia XA1", "price" => "P10,690"),
  7. array("name" => "ASUS ZenFone Max Pro", "price" => "P9,495")
  8. );
  9.  
  10. if(ISSET($_POST['toggle'])){
  11. $sortKey = array();
  12. $sort = $_POST['sort'];
  13. foreach($phones as $phone){
  14. foreach($phone as $key=>$value){
  15. if(!isset($sortKey[$key])){
  16. $sortKey[$key] = array();
  17. }
  18. $sortKey[$key][] = $value;
  19. }
  20. }
  21.  
  22. $sortby = "name";
  23.  
  24. if($sort == "SORT_ASC"){
  25. array_multisort($sortKey[$sortby],SORT_ASC,$phones);
  26. }else if($sort == "SORT_DESC"){
  27. array_multisort($sortKey[$sortby],SORT_DESC,$phones);
  28. }
  29.  
  30. foreach($phones as $key => $values){
  31. ?>
  32. <tr>
  33. <td><?php echo $values['name']?></td>
  34. <td><?php echo $values['price']?></td>
  35. </tr>
  36. <?php
  37. }
  38. }else{
  39. foreach($phones as $key => $values){
  40.  
  41. ?>
  42. <tr>
  43. <td><?php echo $values['name']?></td>
  44. <td><?php echo $values['price']?></td>
  45. </tr>
  46. <?php
  47. }
  48. }
  49. ?>
There you have it we successfully created Sort Multidimensional Array By Key Using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Tags

Add new comment