【问题标题】:Group and order array of objects in php by string value按字符串值对php中的对象数组进行分组和排序
【发布时间】:2020-02-05 14:03:25
【问题描述】:

我有一个看起来像这样的对象数组,请注意它们已经通过 MYSQL 由 create_time 排序:

array (size=5)
  0 => 
    object(stdClass)[38]
      public 'vacancy_id' => int 5
      public 'title' => string 'test title' (length=10)
      public 'create_time' => string '2018-10-05 11:15:34' (length=19)
      public 'language_iso' => string 'NL' (length=2)
  1 => 
    object(stdClass)[42]
      public 'vacancy_id' => int 9
      public 'title' => string 'test title 5' (length=12)
      public 'create_time' => string '2018-08-05 11:15:34' (length=19)
      public 'language_iso' => string 'EN' (length=2)
  2 => 
    object(stdClass)[40]
      public 'vacancy_id' => int 7
      public 'title' => string 'test title 3' (length=12)
      public 'create_time' => string '2018-08-05 11:15:34' (length=19)
      public 'language_iso' => string 'FR' (length=2)
  3 => 
    object(stdClass)[39]
      public 'vacancy_id' => int 6
      public 'title' => string 'test title 2' (length=12)
      public 'create_time' => string '2018-06-05 11:15:34' (length=19)
      public 'language_iso' => string 'FR' (length=2)
  4 => 
    object(stdClass)[41]
      public 'vacancy_id' => int 8
      public 'title' => string 'test title 4' (length=12)
      public 'create_time' => string '2018-02-05 11:15:34' (length=19)
      public 'language_iso' => string 'NL' (length=2)

在我的页面上,我正在实现排序功能。 (目前)有 4 个排序选项:

  • 通过 create_time(这也是默认排序,就像在 MySQL 中所做的那样。我一直希望保持这种排序)。
  • 通过 NL 语言 iso
  • 按FR语言iso
  • 按 EN 语言 iso

在上面给出的数组示例中,假设我想按“FR 语言 iso”排序。这意味着我希望我的结果集如下所示:

array (size=5)
  0 => 
    object(stdClass)[40]
      public 'vacancy_id' => int 7
      public 'title' => string 'test title 3' (length=12)
      public 'create_time' => string '2018-08-05 11:15:34' (length=19)
      public 'language_iso' => string 'FR' (length=2)
  1 => 
    object(stdClass)[39]
      public 'vacancy_id' => int 6
      public 'title' => string 'test title 2' (length=12)
      public 'create_time' => string '2018-06-05 11:15:34' (length=19)
      public 'language_iso' => string 'FR' (length=2)
  2 => 
    object(stdClass)[38]
      public 'vacancy_id' => int 5
      public 'title' => string 'test title' (length=10)
      public 'create_time' => string '2018-10-05 11:15:34' (length=19)
      public 'language_iso' => string 'NL' (length=2)
  3 => 
    object(stdClass)[42]
      public 'vacancy_id' => int 9
      public 'title' => string 'test title 5' (length=12)
      public 'create_time' => string '2018-08-05 11:15:34' (length=19)
      public 'language_iso' => string 'EN' (length=2)
  4 => 
    object(stdClass)[41]
      public 'vacancy_id' => int 8
      public 'title' => string 'test title 4' (length=12)
      public 'create_time' => string '2018-02-05 11:15:34' (length=19)
      public 'language_iso' => string 'NL' (length=2)

所以在这个例子中,结果必须包含:

  • language_iso FR 在顶部但仍在其中的结果,按 create_time 排序
  • language_iso EN 和 NL 的结果位于 FR 结果之下,但也在其中,按 create_time 排序

我一直在使用 php 的 usort 函数中的自定义函数完全运行空白。

【问题讨论】:

    标签: php sorting usort


    【解决方案1】:

    您可以(并且可能应该)完全从 MySQL 处理此排序要求:

    SELECT vacancy_id, title, create_time, language_iso
    FROM yourTable
    ORDER BY
        IF(language_iso = 'FR', 0, 1),
        create_time;
    

    上面的ORDER BY 子句将首先涵盖法语记录,然后是所有其他语言,create_time 涵盖作为第二步的排序。

    虽然您可以尝试在 PHP 中对给定的数组/结果集进行排序,但实际上您可能希望始终查询,因为基础数据可能会变得陈旧。

    【讨论】:

    • 这样完美解决问题!我之前一直在寻找一个mysql的解决方案,但我找不到它,并认为用php解决它会更容易。原来我错了!再次感谢您提供非常简单的解决方案。
    • @Dennis 如果您有更复杂的要求,并且想按特定顺序排列每种语言,请考虑使用FIELD,例如ORDER BY FIELD(language_iso, 'FR', 'EN', 'NL'),这会将法语放在英语之前,荷兰之前。
    猜你喜欢
    • 2019-11-24
    • 2021-06-27
    相关资源
    最近更新 更多