In this tutorial we will create a
Sorting Array Key using PHP. This code will sort array value when user select the proper order in the table. The code use PHP
POST method that call a specific functions in order to sort the array value using
array_multisort by setting the parameter with array index position and array order type. This is a user-friendly kind of program feel free to modify it.
We will be using
PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites and it has a modern technology that can easily be use by the next generation.
Getting 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.
And, 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.
<!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 - Sorting Array Key</h3>
<hr style="border-top:1px dotted #ccc;"/>
<form method="POST" action="">
<div class="form-inline">
<select name="key" required="required" class="form-control">
<option value="sort_asc">Ascending</option>
<option value="sort_desc">Descending</option>
</select>
<button name="sort" class="btn btn-primary">Sort</button>
<div>
</form>
<br />
<div class="col-md-6">
<table class="table table-bordered">
<thead class="alert-info">
<tr>
<th>Member Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php include 'sort.php'?>
</tbody>
</table>
</div>
</div>
</body>
</html>
Creating the Main Function
This code contains the main function of the application. This code will sort array value when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as
sort.php
<?php
array("name" => "Tony Stark", "address" => "Japan"), array("name" => "Peter Piper", "address" => "New York"), array("name" => "Carl Roger", "address" => "London"), array("name" => "Isko Sky", "address" => "England"), array("name" => "Zia Adantos", "address" => "Egypt") );
if(ISSET($_POST['sort'])){ $sort = $_POST['key'];
foreach($members as $member){
foreach($member as $key=>$value){
if(!isset($sortKey[$key])){ $sortKey[$key] = array(); }
$sortKey[$key][] = $value;
}
}
$sortby = "name";
if($sort == "sort_asc"){
}else if($sort == "sort_desc"){
}
foreach($members as $key => $values){
?>
<tr>
<td><?php echo $values['name']?></td>
<td><?php echo $values['address']?></td>
</tr>
<?php
}
}else{
foreach($members as $key => $values){
?>
<tr>
<td><?php echo $values['name']?></td>
<td><?php echo $values['address']?></td>
</tr>
<?php
}
}
?>
There you have it we successfully created
Sorting Array 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!