简体   繁体   中英

Sorting Php associative array based on key value

Ok guys, I have an php associative array in the following format .

groupdata: Array
(
    [0] => Array
        (
            [id] => 3324
            [name] => Testme2
            [creationDate] => 31-MAR-14 04.18.29.618000 PM

        )

    [1] => Array
        (
            [id] => 3325
            [name] => paris
            [creationDate] => 31-MAR-14 06.43.28.291000 PM
        )

    [2] => Array
        (
            [id] => 3236
            [name] => GL_HF
            [creationDate] => 29-MAY-14 12.42.01.444000 PM
        )

    [3] => Array
        (
            [id] => 3251
            [name] => attempting
            [creationDate] => 24-FEB-14 03.47.54.732000 PM
        )

    [4] => Array
        (
            [id] => 3272
            [name] => Testme
            [creationDate] => 03-MAR-14 11.24.58.671000 AM
        )
)

Now I want to sort this array based on their names. How can i achieve this ?

I have tried to use ksort/asort methods but it is not wroking.

The desired array is :

groupdata: Array
(
 [0] => Array
        (
            [id] => 3251
            [name] => attempting
            [creationDate] => 24-FEB-14 03.47.54.732000 PM
        )
[1] => Array
        (
            [id] => 3236
            [name] => GL_HF
            [creationDate] => 29-MAY-14 12.42.01.444000 PM
        )
[2] => Array
        (
            [id] => 3325
            [name] => paris
            [creationDate] => 31-MAR-14 06.43.28.291000 PM
        )
[3] => Array
        (
            [id] => 3272
            [name] => Testme
            [creationDate] => 03-MAR-14 11.24.58.671000 AM
        )
[4] => Array
        (
            [id] => 3324
            [name] => Testme2
            [creationDate] => 31-MAR-14 04.18.29.618000 PM

        )
)
$price = array();
foreach ($groupdata as $key => $row)
{
    $price[$key] = $row['name'];
}
array_multisort($price, SORT_DESC, $groupdata);

Read more array_multisort()

Use usort , so you can define your own way to compare elements:

function cmp($a, $b) {
    if ($a['name'] == $b['name']) {
        return 0;
    }
    return ($a['name'] < $b['name']) ? -1 : 1;
}


usort($your_array, "cmp");

Alternatively, you could use usort() in conjunction to strtolower() . Consider this example:

$groupdata = array(array('id' => 3324,'name' => 'Testme2','creationDate' => '31-MAR-14 04.18.29.618000 PM'),array('id' => 3325,'name' => 'paris','creationDate' => '31-MAR-14 04.18.29.618000 PM'),array('id' => 3236,'name' => 'GL_HF','creationDate' => '31-MAR-14 04.18.29.618000 PM'),array('id' => 3251,'name' => 'attempting','creationDate' => '31-MAR-14 04.18.29.618000 PM'),array('id' => 3272,'name' => 'Testme','creationDate' => '31-MAR-14 04.18.29.618000 PM'),);

function sorter($a, $b) {
    if ($a['name'] == $b['name']){
        return 0;
    }
    return strtolower($a['name']) < strtolower($b['name']) ? -1 : 1;
}

usort($groupdata, 'sorter');

Sample Output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM