按字符串值对 php 中的对象数组进行分组和排序

Group and order array of objects in php by string value

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

排序
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 个排序选项:

在上面给出的数组示例中,假设我想按 "FR language 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)

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

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

您可以(并且可能应该)完全从 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 内的给定 array/result 集进行排序,但实际上您可能希望始终查询,因为基础数据可能变得陈旧。