Sorting Array Key in PHP

By CampCodes Administrator

Updated on:

Sorting Array Key in PHP

In this tutorial, we will create a Sorting Array Key using PHP. This code will sort array value when the user selects the proper order in the table. The system uses the PHP POST method that calls specific functions to sort the array value using array_multisort by setting the parameter with an 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, which is mainly used on any web server, including xamp, wamp, etc. It is being used to any popular websites, and it has a modern technology that can easily be used by the next generation.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the 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 structures, 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
    $members = array(
        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'])){
        $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 = "name";  
 
        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['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
        }
    }
?>
are php arrays ordered? array sort php array_sort php arsort arsort in php asort php how do you sort an array in php? ksort in php ksort multidimensional array php ksort php order array by value order array php php array alphabetical order php array key sort php array order by key php array sort php array sort by field php array sort by key php array sort by key descending php array sort by key value php array sort by value php array sort callback php array_multisort php arsort php asort php asort descending php asort not working php ksort case insensitive php ksort desc php ksort multidimensional array php ksort reverse php natsort multidimensional array php order php order array php order array alphabetically php order by date php reorder array php rsort php sort php sort array php sort array alphabetically php sort array alphabetically by value php sort array by column php sort array by date php sort array by date descending php sort array by date key php sort array by date value php sort array by datetime php sort array by key php sort array by key alphabetically php sort array by key descending php sort array by key value php sort array by key value date php sort array by key value descending php sort array by key value pair php sort array by specific key php sort array by specific key value php sort array by subarray value php sort array by value php sort array by value descending php sort array keep keys php sort array of arrays php sort array of objects php sort array of objects by date php sort array of objects by multiple properties php sort associative array php sort associative array by key php sort associative array by key value php sort associative array by specific key php sort associative array by value php sort descending php sort json php sort json array php sort multidimensional array php sort multidimensional array by another array php sort multidimensional array by column php sort multidimensional array by date php sort multidimensional array by key php sort multidimensional array by key alphabetically php sort multidimensional array by key descending php sort multidimensional array by key value php sort multidimensional array by specific key value php sort multidimensional array by value php sort multidimensional array by value alphabetically php sort multidimensional array by value descending php sort multidimensional associative array php sort multidimensional associative array by value php sort string alphabetically php sorting php sorting array php sorting multidimensional arrays php tutorials php usort sort an array php sort array by key sort array by key javascript sort array by key value sort array by key value javascript sort array by value sort array by value php sort array in php without using function sort array php sort array using for loop in php sort associative array php sort json array by key php sort multidimensional array php sort multidimensional array php by key sort php sort php array by key sort php array by key value sorting array key in php usort in php usort php

Leave a Comment