【问题标题】:How to sort Object Array Collection by value in PHP如何在 PHP 中按值对对象数组集合进行排序
【发布时间】:2018-12-17 10:55:14
【问题描述】:

所以我从数据库中得到了这个结果:

object(Illuminate\Support\Collection)#538 (1) {
  ["items":protected]=>
    array(3) {
        [0]=>
        object(stdClass)#536 (3) {
           ["col_header"]=>
           string(7) "other_1"
           ["col_order"]=>
           int(12)
           ["data"]=>
           string(13) "asdgasdgfasdg"
        }
        [1]=>
        object(stdClass)#545 (3) {
           ["col_header"]=>
           string(7) "other_2"
           ["col_order"]=>
           int(10)
           ["data"]=>
           string(10) "dfhgsdhgsd"
       }
       [2]=>
       object(stdClass)#533 (3) {
           ["col_header"]=>
           string(7) "other_3"
           ["col_order"]=>
           int(11)
           ["data"]=>
           string(1) "s"
       }
}

现在,如何根据 col_order 的值对其结果进行排序?结果应该是这样的:

object(Illuminate\Support\Collection)#538 (1) {
  ["items":protected]=>
    array(3) {
        [0]=>
        object(stdClass)#545 (3) {
           ["col_header"]=>
           string(7) "other_2"
           ["col_order"]=>
           int(10)
           ["data"]=>
           string(10) "dfhgsdhgsd"
        }
        [1]=>
        object(stdClass)#533 (3) {
           ["col_header"]=>
           string(7) "other_3"
           ["col_order"]=>
           int(11)
           ["data"]=>
           string(1) "s"
        }
        [2]=>
        object(stdClass)#536 (3) {
           ["col_header"]=>
           string(7) "other_1"
           ["col_order"]=>
           int(12)
           ["data"]=>
           string(13) "asdgasdgfasdg"
        }
}

我在这里尝试过使用asort(),但它似乎只支持关联数组。有什么办法可以解决这个问题吗?

【问题讨论】:

  • 为什么您没有在查询中使用 orderBy?正如我认为你正在使用 laravel elquent 所以你可以在你的查询中使用 orderBy(''col_order)
  • @MD.JubairMizan 谢谢你。我没有想到!它解决了我的问题,将在我未来的场景中使用答案,

标签: php arrays sorting arraycollection


【解决方案1】:

您可以使用usort()

$test = array(array("col_header" => "other_1",
                    "col_order" => 12,
                    "data" => "asdgasdgfasdg"),
              array("col_header" => "other_2",
                    "col_order" => 10,
                    "data" => "dfhgsdhgsd"),
              array("col_header" => "other_3",
                    "col_order" => 11,
                    "data" => "s"));

usort($test, function($a, $b)
             {
                 if ($a["col_order"] == $b["col_order"])
                     return (0);
                 return (($a["col_order"] < $b["col_order"]) ? -1 : 1);
             });

var_dump($test);

输出:

array (size=3)
  0 => 
    array (size=3)
      'col_header' => string 'other_2' (length=7)
      'col_order' => int 10
      'data' => string 'dfhgsdhgsd' (length=10)
  1 => 
    array (size=3)
      'col_header' => string 'other_3' (length=7)
      'col_order' => int 11
      'data' => string 's' (length=1)
  2 => 
    array (size=3)
      'col_header' => string 'other_1' (length=7)
      'col_order' => int 12
      'data' => string 'asdgasdgfasdg' (length=13)

但我建议您直接从 SQL 查询中对结果进行排序:

SELECT *
FROM yourTable
...
ORDER BY col_order ASC

【讨论】:

    【解决方案2】:

    答案是http://php.net/manual/en/function.usort.php - 它按用户定义的比较函数排序。在以下 sn-p 中将 a 替换为 col_order

    <?php
    
    $class1 = new stdClass();
    $class1->a = 1;
    $class2 = new stdClass();
    $class2->a = 2;
    $class3 = new stdClass();
    $class3->a = 3;
    
    $input = [$class3,$class2,$class1];
    
    var_dump($input);
    
    usort($input, function($a, $b) {
          if ($a->a == $b->a) {
            return 0;
        }
        return ($a->a < $b->a) ? -1 : 1;
    });
    
    var_dump($input);
    

    https://3v4l.org/9ELKp

    当您使用 Illuminate 时,您可能还想查看 https://laravel.com/api/5.5/Illuminate/Database/Query/Builder.html#method_orderBy 并改用它。

    从 cmets 编辑:使用 PHP 7+ 您可以使用 spaceship 运算符来简化比较功能:

    usort($input, function($a, $b) {
        return $a->a <=> $b->a;
    });
    

    【讨论】:

    • 函数体可以是 php7 中带有 spaceship 运算符的 oneliner:return $a-&gt;a &lt;=&gt; $b-&gt;a;
    • @FilipHalaxa 谢谢,我会将其添加到我的答案中。
    猜你喜欢
    • 2017-11-20
    • 2010-09-29
    • 1970-01-01
    • 2017-05-01
    • 2020-11-15
    • 1970-01-01
    • 2021-10-21
    • 2019-11-02
    • 2010-12-04
    相关资源
    最近更新 更多