<!DOCTYPE html> <html lang="en"> <head> <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://sourcecodester.com">Sourcecodester</a> </div> </nav> <div class="col-md-3"></div> <div class="col-md-6 well"> <h3 class="text-primary">PHP - Sort Multidimensional Array By Key</h3> <hr style="border-top:1px dotted #ccc;"/> <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-primary">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>
<?php ); $sort = $_POST['sort']; foreach($phones as $phone){ foreach($phone as $key=>$value){ } $sortKey[$key][] = $value; } } $sortby = "name"; if($sort == "SORT_ASC"){ }else if($sort == "SORT_DESC"){ } foreach($phones as $key => $values){ ?> <tr> <td><?php echo $values['name']?></td> <td><?php echo $values['price']?></td> </tr> <?php } }else{ foreach($phones as $key => $values){ ?> <tr> <td><?php echo $values['name']?></td> <td><?php echo $values['price']?></td> </tr> <?php } } ?>