Today, We want to share with you PHP Sorting Multidimensional Array By Key.
In this post we will show you php sort associative array by column, hear for Multi dimensional array sorting in php we will give you demo and example for implement.
In this post, we will learn about Sorting Multi-Dimensional Arrays in PHP with an example.
PHP Sorting Multidimensional Array By Key
There are the Following The simple About PHP Sorting Multidimensional Array Example Full Information With Example and source code.
Making The Interface
index.php
<!DOCTYPE html> <html lang="en"> <head> <title>Multi dimensional array sorting in php</title> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" /> <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand" href="https://www.pakainfo.com/php-sorting-multidimensional-array/%3Ca%20href=">Pakainfo</a> </a> </div> </nav> <div class="col-md-3"></div> <div class="col-md-6 well pakainfo"> <h3 class="text-success">PHP - Multi dimensional array sorting</h3> <hr style="border-top:2px dotted #3d3d3d;"/> <form method="POST" action=""> <div class="form-inline"> <select name="sort" required="required" class="form-control"> <option value="SORT_ASC">ASCENDING</option> <option value="SORT_DESC">DESCENDING</option> </select> <button name="toggle" class="btn btn-success">Sort</button> <div> </form> <br /> <table class="table table-bordered"> <thead> <tr> <th>Name</th> <th>Price</th> </tr> </thead> <tbody> <?php include 'sort.php'?> </tbody> </table> </div> </body> </html>
Creating Main Function – sort.php
<?php $laptops = array( array("name" => "Lenovo z570", "price" => "P58,35,990"), array("name" => "Dell X", "price" => "P53,589300"), array("name" => "Lenovo g578", "price" => "P11,98952.99"), array("name" => "Sony Xperia", "price" => "P10,64,590"), array("name" => "ASUS Laptoo Max Pro", "price" => "P9,985,495") ); if(ISSET($_POST['toggle'])){ $lapotpSortK = array(); $sort = $_POST['sort']; foreach($laptops as $laptop){ foreach($laptop as $key=>$value){ if(!isset($lapotpSortK[$key])){ $lapotpSortK[$key] = array(); } $lapotpSortK[$key][] = $value; } } $laptop_sort = "name"; if($sort == "SORT_ASC"){ array_multisort($lapotpSortK[$laptop_sort],SORT_ASC,$laptops); }else if($sort == "SORT_DESC"){ array_multisort($lapotpSortK[$laptop_sort],SORT_DESC,$laptops); } foreach($laptops as $key => $values){ ?> <tr> <td><?php echo $values['name']?></td> <td><?php echo $values['price']?></td> </tr> <?php } }else{ foreach($laptops as $key => $values){ ?> <tr> <td><?php echo $values['name']?></td> <td><?php echo $values['price']?></td> </tr> <?php } } ?>
Multiple Image Upload in php with Database
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about php sort multidimensional array by column.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.