按日期排序数组

时间:2012-02-08 23:17:44

标签: php mysql arrays

我有一个问题。我正在通过从mysql获取数据并将三个查询结果合并到一个数组中来构建数组。

我把数据放到这样的数组中:

while ($a = mysql_fetch_object($activities)) {
            $a->type = 1;
            $array1[] = $a;
        }
        while ($k = mysql_fetch_object($fuups)) {
        $k->type = 2;
            $array1[] = $k;
        }
        while ($x = mysql_fetch_object($refuups)) {
            $x->type = 3;
            $array1[] = $x;
        }

        return (object)$array1;

返回如下内容:

stdClass Object
(
    [0] => stdClass Object
        (
            [added] => 2012-01-17 07:33:53
            [type] => 1
        )

    [1] => stdClass Object
        (
            [added] => 2012-01-13 06:36:22
            [type] => 1
        )

    [2] => stdClass Object
        (
            [added_type_2] => 2012-01-09 04:01:12
            [type] => 2
        )

    [3] => stdClass Object
        (
            [added_type_2] => 2012-02-08 02:08:32
            [type] => 2
        )

    [4] => stdClass Object
        (
            [added_type_2] => 2012-01-25 00:09:08
            [type] => 2
        )

    [5] => stdClass Object
        (
            [added_type_3] => 2012-01-23 00:09:08
            [type] => 3
        )

    [6] => stdClass Object
        (
            [added_type_3] => 2012-01-22 00:09:08
            [type] => 3
        )

)

我试过像asort,ksort这样的东西,排序但没有运气。还通过“添加desc的订单”获取日期谢谢

3 个答案:

答案 0 :(得分:2)

你要做的是对多维数组进行排序,你可以在谷歌上找到很多这方面的内容。一个很好的解决方案就是:

// Sort the multidimensional array
usort($results, "custom_sort");

// Define the custom sort function
function custom_sort($a,$b) {
     return $a['some_sub_var']>$b['some_sub_var'];
}

编辑1:

对于那些怀疑此代码是否有效的评论中的用户,请随意尝试(我甚至在测试目的的重复日期中添加):

function custom_sort($a,$b) {
        return $a['added']>$b['added'];
}

$arrayToSort = array(
                    array(
                        "added" => "2012-01-17 07:33:53",
                        "type" => "1"
                    ),
                    array(
                        "added" => "2012-01-13 06:36:22",
                        "type" => "1"
                    ),
                    array(
                        "added" => "2012-01-09 04:01:12",
                        "type" => "2"
                    ),
                    array(
                        "added" => "2012-02-08 02:08:32",
                        "type" => "2"
                    ),
                    array(
                        "added" => "2012-01-25 00:09:08",
                        "type" => "2"
                    ),
                    array(
                        "added" => "2012-01-13 06:36:22",
                        "type" => "1"
                    ),
                    array(
                        "added" => "2012-01-13 06:36:22",
                        "type" => "1"
                    ),
                    array(
                        "added" => "2012-01-23 00:09:08",
                        "type" => "3"
                    ),
                    array(
                        "added" => "2012-01-22 00:09:08",
                        "type" => "3"
                    )
                );
usort($arrayToSort, "custom_sort");

echo '<pre>';
print_r($arrayToSort);
echo '</pre>';

快速测试的好地方是转到http://writecodeonline.com/php/

答案 1 :(得分:0)

在选择而不是尝试自行排序数组时,您可能应该使用UNION。无论如何,如果你必须这样使用usort

function cmp( $a, $b){
  if( !($a instanceOf stdClass) && !($b instanceOf stdClass)){
    return 0;
  }

  // Check object
  if(  !($a instanceOf stdClass)){
    return -1;
  }
  if(  !($b instanceOf stdClass)){
    return 1;
  }

  $aVal = NULL;
  if( isset( $a->added)){
    $aVal = $a->added;
  } elseif( isset( $a->added_type_2)){
    $aVal = $a->added_type_2;
  } ...

  // The same for b
  if( ($aVal == NULL) && ($bVal == NULL)){
    return 0;
  }

  if( $aVal == NULL){
    return -1;
  }

  if( $bVal == NULL){
    return 1;
  }

  if( $aVal == $bVal){
    return 0;
  }

  return ($aVal > $bVal) ? 1 : -1;

}

usort( $array, 'cmp');

正如您可能会看到,在确保对象具有正确的类型和正确的值时,如何比较对象可能会变得复杂。选择mysql列时,至少应使用SELECT added_type_2 AS added使列名更紧凑,条件更简单。

答案 2 :(得分:0)

如果将fetch对象更改为fetch assoc:

,则可以使用usort()
function my_date_sort($a,$b) {
   if(isset($a[0]) && isset($b[0])) {
       $a = strtotime($a[0]); // optionally convert to time
       $b = strtotime($b[0]);
       if($a == $b) return 0; 
       else return ($a > $b) ? 1 : -1;
   } else { // no valid subs, change this to put empty ones on top or bottom
       return 1; // put at bottom, -1 would put at top. 
}


usort($results, 'my_date_sort');
祝你好运......

相关问题