Array Sort In PHP | CampCodes ...

Array Sort in PHP

June 1, 2020
PHP
simple-array-sort-using-php

In this tutorial, we will do Simple Array Sort using PHP. This code will teach you on how to deal with PHP arrays in a simple way. The program will dynamically sort the table entry data when you apply a keyword as a key in the array_multisort() function. You can modify this program and use it on your working programs.

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 copy of the way 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">Simple Array Sort Using PHP</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-4"></div>
        <div class="col-md-8">
            <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 />
            <table class="table table-bordered">
                <thead class="alert-info">
                    <tr>
                        <th>Firstname</th>
                        <th>Lastname</th>
                        <th>Address</th>
                    </tr>
                </thead>
                <tbody>
                    <?php include 'sorting_array.php'?>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>

Creating the Main Function

This code contains the main function of the application. The code will sort the array index positions base on the given key. To make this just copy and write these block of codes below inside the text editor, then save it as sorting_array.php

<?php
    $members = array(
        array("firstname" => "Tony", "lastname" => "Stark",  "address" => "Japan"),
        array("firstname" => "Peter", "lastname" => "Piper",  "address" => "New York"),
        array("firstname" => "Carl", "lastname" => "Roger",  "address" => "London"),
        array("firstname" => "Isko",  "lastname" => "Sky", "address" => "England"),
        array("firstname" => "Zia", "lastname" => "Adantos",  "address" => "Egypt")
    );
 
    if(ISSET($_POST['sort'])){
        $sortKey = array(); 
        $sort = $_POST['key'];
        foreach($members as $member){ 
            foreach($member as $key=>$value){ 
                if(!isset($sortKey[$key])){ 
                    $sortKey[$key] = array(); 
                } 
                $sortKey[$key][] = $value; 
            } 
        } 
 
        $sortby = "lastname";  
 
        if($sort == "sort_asc"){
            array_multisort($sortKey[$sortby],SORT_ASC,$members); 
        }else if($sort == "sort_desc"){
            array_multisort($sortKey[$sortby],SORT_DESC,$members); 
        }
 
        foreach($members as $key => $values){
?>
            <tr>
                <td><?php echo $values['firstname']?></td>
                <td><?php echo $values['lastname']?></td>
                <td><?php echo $values['address']?></td>
            </tr>
<?php	
        }
    }else{
        foreach($members as $key => $values){
 
?>
            <tr>
                <td><?php echo $values['firstname']?></td>
                <td><?php echo $values['lastname']?></td>
                <td><?php echo $values['address']?></td>
            </tr>
<?php
        }
    }
?>

There you have it we successfully created Simple Array Sort 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. Happy Coding!

Download Here

Leave a Reply

Your email address will not be published. Required fields are marked *