开发者

Sort Multi Array in PHP

Does anybody have an idea how can I sort this array by key (date) in PHP?

Array
(   
    [2011-02-16] => Array
        (
            [date] => 2011-02-16
            [num] => 2
        )

    [2011-02-11] => Array
        (
            [date] => 2011-02-11
            [num] => 0
        )

    [2011-02-17] => Array
        (
            [date] => 2011-02-17
            [num] => 0
        )

    [开发者_运维技巧2011-02-18] => Array
        (
            [date] => 2011-02-18
            [num] => 0
        )

)


Use the uasort function, which is user customizable sorting. Like this:

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

uasort($your_array, "cmp");


have you tried ksort? http://www.php.net/manual/en/function.ksort.php


Sure thing. I answered this exact post yesterday, too.

Sorting 2 arrays to get the highest in one and lowest in another

In your case, it will be something like...

$myArray= subval_sort($myArray,'date'); 

function subval_sort($a,$subkey) {
    foreach($a as $k=>$v) {
        $b[$k] = strtolower($v[$subkey]);
    }
    asort($b);
    foreach($b as $key=>$val){
        $c[] = $a[$key];
    }
    return $c;
}

EDIT

The answer by shamittomar is better though :)


This should help you brush up on the basics of array sorting in PHP

http://www.the-art-of-web.com/php/sortarray/

Something like this would sort your problem however:

usort($array, "cmp");

function cmp($a, $b){ 
    return strcmp($b['date'], $a['date']); 


Well, as sorting on the key would do it, ksort() would work for you!

But if you really want to sort on the date element, you would use uasort() with a sort function like this

  function compare($a, $b)
  {
      if ($a['date']==$b['date']) return 0;
      return ($a['date']>$b['date'])?1:-1;
  }

  uasort($myarray, 'compare');


Since your array already has the date in the keys, why not just use ksort? The string comparison should work fine since you're using YYYY-MM-dd format.


http://www.php.net/manual/en/function.array-multisort.php

Would example #3 suit your needs?


What you want is UASORT.

http://us3.php.net/manual/en/function.uasort.php

function would be like:

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


A better answer would be to use uksort which is used to sort keys with a user-defined function (considering that these dates cannot always be compared and cannot be sorted with ksort without first applying strtotime to the keys):

function sort_by_date_keys($date_key1, $date_key2) {
    // reverse the order of the $date_keys for "oldest to newest"
    return strtotime($date_key2) - strtotime($date_key1);
);

uksort($array, 'sort_by_date_keys');

This method is more defined than uasort as it was tailored for keys.

Example:

$array = array(
    '1/1/12' => 'foo1',
    '1/1/13' => 'foo2'
);
uksort($array, 'sort_by_date_keys');

// output
$array = array(
    '1/1/13' => 'foo2',
    '1/1/12' => 'foo1'
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜