【问题标题】:Sort an array of objects fetched from database对从数据库中获取的对象数组进行排序
【发布时间】:2014-12-15 11:56:55
【问题描述】:

我正在使用 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 行来自第一个表,最后一行来自第二个表。

【问题讨论】:

  • 为什么不在 SQL 查询中排序呢? '$results = $this->db->query($query1." UNION ".$query2 ." ORDER BY date_dubmitted");'
  • 谢谢。那行得通。
  • 将我的答案标记为实际答案,以便人们在遇到此类问题时停止评论或进一步搜索。 :)
  • 但我还是不明白为什么我的逻辑没有给出所需的结果?
  • php中日期格式的区别是个大问题。阅读更多相关内容,您可能会在那里找到答案。它位于 PHP 的 date_diff 函数中

标签: php arrays codeigniter sorting


【解决方案1】:

对于不读 cmets 的人。所要做的就是将 SQL 中的“Order By”属性添加到您的查询中,如下所示;

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

【讨论】:

    【解决方案2】:

    您应该首先将日期时间字符串转换为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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      相关资源
      最近更新 更多