This function is used to sort the array based on some columns.



Many moments have come where I had a PHP array from a database, and I had to sort it based on some array key.

Unsorted PHP Array

Syntax:

<?php 
$Array = array(
    array(
        'ProfilePic' => '',
        'UnreadCount' => '0',
        'RefrenceType' => 'Group',
        'DisplayName' => 'John and 3 others.',
        'ModifiedDate' => '2014-09-26 06:57:46',
        'EntryDate' => '2014-09-26 10:04:45'
    ),
    array(
        'ProfilePic' => '',
        'UnreadCount' => '3',
        'RefrenceType' => 'Group',
        'DisplayName' => 'Alex and 2 others.',
        'ModifiedDate' => '2014-09-26 10:05:38',
        'EntryDate' => NULL
    ),
    array(
        'UnreadCount' => '2',
        'RefrenceType' => 'User',
        'ProfilePic' => 'http => //www.example.com/GetUserImage/1c33a3a47d7ff48fbbced5bb6ea3face',
        'DisplayName' => 'Riya',
        'EntryDate' => '2014-09-28 10:14:27',
        'ModifiedDate' => '2014-09-28 12:47:53'
    ),
    array(
        'UnreadCount' => '4',
        'RefrenceType' => 'User',
        'ProfilePic' => 'http => //www.example.com/GetUserImage/1b01b7925d3f7dd230c166ee537b7f85',
        'DisplayName' => 'Vandana',
        'EntryDate' => '2014-09-26 09:56:11',
        'ModifiedDate' => '2014-09-22 13:12:54'
    ),
    array(
        'UnreadCount' => '0',
        'RefrenceType' => 'User',
        'ProfilePic' => 'http => //www.example.com/GetUserImage/64bb6ac3074e63791d6291b72bd5e42b',
        'DisplayName' => 'Celly',
        'EntryDate' => '2014-09-26 09:54:53',
        'ModifiedDate' => '2014-09-22 12:52:22'
    ),
    array(
        'UnreadCount' => '0',
        'RefrenceType' => 'User',
        'ProfilePic' => 'http => //www.example.com/GetUserImage/995054e37437feb61d4d2b0b84cb71c6',
        'DisplayName' => 'Vijay',
        'EntryDate' => '2014-09-26 07:30:42',
        'ModifiedDate' => '2014-09-22 12:50:40'
    ),
    array(
        'UnreadCount' => '0',
        'RefrenceType' => 'User',
        'ProfilePic' => 'http://www.example.com/GetUserImage/27c556a98b25953c1df96d01d7262c00',
        'DisplayName' => 'Vikas M.',
        'EntryDate' => '2014-09-26 18:08:51',
        'ModifiedDate' => '2014-09-22 13:21:05'
    )
);
?>

PHP Array Sort Function

Syntax:

<?php
function phparraysort($Array, $SortBy=array(), $Sort = SORT_REGULAR) {
    if (is_array($Array) && count($Array) > 0 && !empty($SortBy)) {
            $Map = array();                     
            foreach ($Array as $Key => $Val) {
                $Sort_key = '';                         
                                foreach ($SortBy as $Key_key) {
                                        $Sort_key .= $Val[$Key_key];
                                }                
                $Map[$Key] = $Sort_key;
            }
            asort($Map, $Sort);
            $Sorted = array();
            foreach ($Map as $Key => $Val) {
                $Sorted[] = $Array[$Key];
            }
            return array_reverse($Sorted);
    }
    return $Array;
}

$Array = phparraysort($Array, array('UnreadCount','EntryDate','ModifiedDate'));
print_r($Array);
?>

In this example, I have sorted an array by multiple column names UnreadCount, EntryDate, and ModifiedDate.

Sorted PHP Array

Program Output:

Array
(
    [0] => Array
        (
            [UnreadCount] => 4
            [RefrenceType] => User
            [ProfilePic] => http => //www.example.com/GetUserImage/1b01b7925d3f7dd230c166ee537b7f85
            [DisplayName] => Vandana
            [EntryDate] => 2014-09-26 09:56:11
            [ModifiedDate] => 2014-09-22 13:12:54
        )

    [1] => Array
        (
            [ProfilePic] => 
            [UnreadCount] => 3
            [RefrenceType] => Group
            [DisplayName] => Alex and 2 others.
            [ModifiedDate] => 2014-09-26 10:05:38
            [EntryDate] => 
        )

    [2] => Array
        (
            [UnreadCount] => 2
            [RefrenceType] => User
            [ProfilePic] => http => //www.example.com/GetUserImage/1c33a3a47d7ff48fbbced5bb6ea3face
            [DisplayName] => Riya
            [EntryDate] => 2014-09-28 10:14:27
            [ModifiedDate] => 2014-09-28 12:47:53
        )

    [3] => Array
        (
            [UnreadCount] => 0
            [RefrenceType] => User
            [ProfilePic] => http://www.example.com/GetUserImage/27c556a98b25953c1df96d01d7262c00
            [DisplayName] => Vikas M.
            [EntryDate] => 2014-09-26 18:08:51
            [ModifiedDate] => 2014-09-22 13:21:05
        )

    [4] => Array
        (
            [ProfilePic] => 
            [UnreadCount] => 0
            [RefrenceType] => Group
            [DisplayName] => John and 3 others.
            [ModifiedDate] => 2014-09-26 06:57:46
            [EntryDate] => 2014-09-26 10:04:45
        )

    [5] => Array
        (
            [UnreadCount] => 0
            [RefrenceType] => User
            [ProfilePic] => http => //www.example.com/GetUserImage/64bb6ac3074e63791d6291b72bd5e42b
            [DisplayName] => Celly
            [EntryDate] => 2014-09-26 09:54:53
            [ModifiedDate] => 2014-09-22 12:52:22
        )

    [6] => Array
        (
            [UnreadCount] => 0
            [RefrenceType] => User
            [ProfilePic] => http => //www.example.com/GetUserImage/995054e37437feb61d4d2b0b84cb71c6
            [DisplayName] => Vijay
            [EntryDate] => 2014-09-26 07:30:42
            [ModifiedDate] => 2014-09-22 12:50:40
        )

)


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram