对从数据库中提取的对象数组进行排序

时间:2014-12-15 11:56:56

标签: php arrays codeigniter sorting

我正在使用codeigniter。在我的一个模型中,我定义了一个函数,该函数使用UNION从两个表中检索数据库中的数据。我希望我检索到的对象数组按照< date_submitted'进行排序。属性。我尝试使用以下代码,但它没有显示所需的结果。

这是我的代码

    $this->db->select("*");
    $this->db->from("artwork");
    $this->db->get(); 
    $query1 = $this->db->last_query();

    $this->db->select("*");
    $this->db->from("blog");
    $this->db->get(); 
    $query2 =  $this->db->last_query();

    $results = $this->db->query($query1." UNION ".$query2);

    function cmp($a, $b)
    {
        if ($a->date_submitted == $b->date_submitted) {
            return 0;
        }
        return ($a->date_submitted < $b->date_submitted) ? -1 : 1;
    }
    usort($results->result(), "cmp");
    var_dump($results->result());

它提供以下输出

array (size=7)
  0 => 
    object(stdClass)[31]
      public 'id' => string '1' (length=1)
      public 'user_id' => string '2' (length=1)
      public 'title' => string 'My Painting2' (length=12)
      public 'category' => string '1' (length=1)
      public 'date_submitted' => string '2014-09-26 23:00:09' (length=19)

  1 => 
    object(stdClass)[32]
      public 'id' => string '2' (length=1)
      public 'user_id' => string '2' (length=1)
      public 'title' => string 'Second artwork' (length=14)
      public 'category' => string '2' (length=1)
      public 'date_submitted' => string '2014-09-27 01:09:03' (length=19)
  2 => 
    object(stdClass)[33]
      public 'id' => string '3' (length=1)
      public 'user_id' => string '2' (length=1)
      public 'title' => string 'Test Title' (length=10)
      public 'category' => string '2' (length=1)
      public 'date_submitted' => string '2014-10-12 01:19:34' (length=19)
  3 => 
    object(stdClass)[34]
      public 'id' => string '4' (length=1)
      public 'user_id' => string '2' (length=1)
      public 'title' => string 'Test Title' (length=10)
      public 'category' => string '3' (length=1)
      public 'date_submitted' => string '2014-10-12 02:54:57' (length=19)
  4 => 
    object(stdClass)[35]
      public 'id' => string '8' (length=1)
      public 'user_id' => string '2' (length=1)
      public 'title' => string 'test' (length=4)
      public 'category' => string '3' (length=1)
      public 'date_submitted' => string '2014-11-13 16:49:06' (length=19)
  5 => 
    object(stdClass)[36]
      public 'id' => string '9' (length=1)
      public 'user_id' => string '2' (length=1)
      public 'title' => string 'Hello World' (length=11)
      public 'category' => string '3' (length=1)
      public 'date_submitted' => string '2014-12-06 22:15:46' (length=19)
  6 => 
    object(stdClass)[37]
      public 'id' => string '5' (length=1)
      public 'user_id' => string '2' (length=1)
      public 'title' => string 'My first blog' (length=13)
      public 'category' => string '2' (length=1)
      public 'date_submitted' => string '2014-10-12 21:26:13' (length=19)

前6行来自第一张表,最后一行来自第2张表。

2 个答案:

答案 0 :(得分:0)

您应该首先将日期时间字符串转换为unix时间戳并比较:

function cmp($a, $b)
    {
        $firstTimeStamp = strtotime( $a->date_submitted );
        $secondTimeStamp = strtotime( $b->date_submitted );

        if ( $firstTimeStamp == $secondTimeStamp ) {
            return 0;
        }

        return ( $firstTimeStamp < $secondTimeStamp ) ? -1 : 1;
    }

答案 1 :(得分:0)

对于不阅读评论的人。所有必须做的就是将SQL中的“Order By”属性添加到您的查询中,如此;

$results = $this->db->query($query1." UNION ".$query2 ." ORDER BY date_dubmitted")