【问题标题】:php sort an object by two criteria?php按两个标准对对象进行排序?
【发布时间】:2010-03-03 00:38:59
【问题描述】:

尝试根据 (1) 深度和 (2) 权重对这个对象数组进行排序,但不确定如何修改我正在使用的函数以包含这个其他级别...

我正在使用这个功能:

function cmp( $a, $b ) {
    if(  $a->weight ==  $b->weight ){
        return 0 ;
    }
    return ($a->weight < $b->weight) ? -1 : 1;
}

然后这样做:

$menu = get_tree(4, $tid, -1, 2);
usort($menu, 'cmp');

这将根据权重对数组进行准确排序,但我需要添加另一个级别的排序。使数组先按深度排序,再按权重排序。

因此,如果原始数组如下所示:

    Array
(
    [0] => stdClass Object
        (
            [tid] => 24
            [name] => Sample
            [weight] => 3
            [depth] => 0
        )

    [1] => stdClass Object
        (
            [tid] => 66
            [name] => Sample Subcategory
            [weight] => 0
            [depth] => 1
        )

    [2] => stdClass Object
        (
            [tid] => 67
            [name] => Another Example
            [weight] => 1
            [depth] => 0
        )

    [3] => stdClass Object
        (
            [tid] => 68
            [name] => Subcategory for Another Example
            [weight] => 1
            [depth] => 1
        )

    [4] => stdClass Object
        (
            [tid] => 22
            [name] => A third example master category
            [weight] => 0
            [depth] => 0
        )

我可以先按深度排序,然后按重量排序,结果如下:

Array
(
    [0] => stdClass Object
        (
            [tid] => 22
            [name] => A third example master category
            [weight] => 0
            [depth] => 0
        )

    [1] => stdClass Object
        (
            [tid] => 67
            [name] => Another Example
            [weight] => 1
            [depth] => 0
        )

    [2] => stdClass Object
        (
            [tid] => 24
            [name] => Sample
            [weight] => 3
            [depth] => 0
        )

    [3] => stdClass Object
        (
            [tid] => 66
            [name] => Sample Subcategory
            [weight] => 0
            [depth] => 1
        )

    [4] => stdClass Object
        (
            [tid] => 68
            [name] => Subcategory for Another Example
            [weight] => 1
            [depth] => 1
        )

【问题讨论】:

    标签: php object sorting


    【解决方案1】:
    function cmp( $a, $b )
    {
      if ($a->depth == $b->depth)
      {
        if($a->weight == $b->weight) return 0 ;
        return ($a->weight < $b->weight) ? -1 : 1;
      }
      else
        return ($a->depth < $b->depth) ? -1 : 1;
    }
    

    【讨论】:

      【解决方案2】:

      比较数字时,你可以简单地将它们相减

      function cmp($a, $b) {
         $d = $a->depth - $b->depth;
         return $d ? $d : $a->weight - $b->weight;
      }
      

      【讨论】:

        【解决方案3】:

        自 2010 年以来,PHP 有了很大的改进。对于这个任务,可以使用 PHP7 的“宇宙飞船运算符”或“三向比较运算符”在 usort() 调用中进行单行比较。

        代码:(Demo)

        usort($menu, function($a, $b) {
            return [$a->depth, $a->weight] <=> [$b->depth, $b->weight];
        });
        

        输出:

        array (
          0 => 
          (object) array(
             'tid' => 22,
             'name' => 'A third example master category',
             'weight' => 0,
             'depth' => 0,
          ),
          1 => 
          (object) array(
             'tid' => 67,
             'name' => 'Another Example',
             'weight' => 1,
             'depth' => 0,
          ),
          2 => 
          (object) array(
             'tid' => 24,
             'name' => 'Sample',
             'weight' => 3,
             'depth' => 0,
          ),
          3 => 
          (object) array(
             'tid' => 66,
             'name' => 'Sample Subcategory',
             'weight' => 0,
             'depth' => 1,
          ),
          4 => 
          (object) array(
             'tid' => 68,
             'name' => 'Subcategory for Another Example',
             'weight' => 1,
             'depth' => 1,
          ),
        )
        

        在左侧数组中写入$a 属性,在右侧写入$b 属性将对ASC 进行排序;反转这些各自的位置以使用 DESC 方向排序。

        扩展:如果你想排序:$weightDESC,然后$nameASC,然后$depthDESC,这样写比较条件数组:

        [$b->weight, $a->name, $b->depth] <=> [$a->weight, $b->name, $a->depth]
        

        从 PHP7.2 开始,可以使用另一种技术:array_multisort() 和多次调用array_column()。在 7.2 之前,array_column() 不能用于对象。)

        代码:(Demo)

        array_multisort(array_column($menu, 'depth'), array_column($menu, 'weight'), $menu);
        

        为了反映我之前的“扩展”中的标准,语法会变长一些,因为每列都需要自己的array_column() 调用,并且需要表达排序方向。

        array_multisort(array_column($menu, 'weight'), SORT_DESC, array_column($menu, 'name'), array_column($menu, 'depth'), SORT_DESC, $menu);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-01-12
          • 2018-02-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多