繁体   English   中英

PHP 自定义 function 用于数组排序而 usort 不起作用

[英]PHP custom function for array sorting wth usort not working

我正在研究 magento 站点 php 代码并尝试使用 label ASC 对数组进行排序。 所以这些值应该按 label 排序。

我为此使用了usort function

但即使我使用 function output 也看不出任何差异。

前任-

        //$info has the array values 
/*which assigned like this 
 $info['options'][] = array(
                        'id'        => $value['value_index'],
                        'label'     => $value['label'],
                        'price'     => $configurablePrice,
                        'oldPrice'  => $this->_prepareOldPrice($value['pricing_value'], $value['is_percent']),
                        'products'  => $productsIndex,
                    );

*/

排序 function 如下

 usort($info['options'], function ($a,$b){
                            return strcmp($a['label'],$b['label']);
                        }
                    ); 
var_dump($info);

即使我排序或不数组显示这个 output

  var spConfig = new Product.Config(array(10) {
      [0]=>
      array(5) {
        ["id"]=>
        string(5) "46050"
        ["label"]=>
        string(3) "110"
        ["price"]=>
        string(1) "0"
        ["oldPrice"]=>
        string(1) "0"
        ["products"]=>
        array(1) {
          [0]=>
          string(5) "95331"
        }
      }
      [1]=>
      array(5) {
        ["id"]=>
        string(5) "46071"
        ["label"]=>
        string(3) "120"
        ["price"]=>
        string(1) "0"
        ["oldPrice"]=>
        string(1) "0"
        ["products"]=>
        array(1) {
          [0]=>
          string(5) "95332"
        }
      }
      [2]=>
      array(5) {
        ["id"]=>
        string(5) "46086"
        ["label"]=>
        string(3) "130"
        ["price"]=>
        string(1) "0"
        ["oldPrice"]=>
        string(1) "0"
        ["products"]=>
        array(1) {
          [0]=>
          string(5) "95333"
        }
      }
      [3]=>
      array(5) {
        ["id"]=>
        string(5) "46112"
        ["label"]=>
        string(3) "140"
        ["price"]=>
        string(1) "0"
        ["oldPrice"]=>
        string(1) "0"
        ["products"]=>
        array(1) {
          [0]=>
          string(5) "95334"
        }
      }          
      [4]=>
      array(5)
      [6]=>
      array(5) {
        ["id"]=>
        string(5) "46455"
        ["label"]=>
        string(2) "60"
        ["price"]=>
        string(1) "0"
        ["oldPrice"]=>
        string(1) "0"
        ["products"]=>
        array(1) {
          [0]=>
          string(5) "95326"
        }
      }
      [5]=>
      array(5) {
        ["id"]=>
        string(5) "46494"
        ["label"]=>
        string(2) "70"
        ["price"]=>
        string(1) "0"
        ["oldPrice"]=>
        string(1) "0"
        ["products"]=>
        array(1) {
          [0]=>
          string(5) "95327"
        }
      }
      [5]=>
      array(5) {
        ["id"]=>
        string(5) "46527"
        ["label"]=>
        string(2) "80"
        ["price"]=>
        string(1) "0"
        ["oldPrice"]=>
        string(1) "0"
        ["products"]=>
        array(1) {
          [0]=>
          string(5) "95328"
        }
      }  
      
    }

我也尝试了其他几个数组键,但没有一个不起作用,任何人都可以帮我排序,谢谢

尝试将它们转换为整数,因为字符串比较与 integer 比较非常不同。 例如, 2按字典顺序排在100之后,但这不是您想要的。

usort ($info['options'], function ($a,$b) {
        $la = intval($a['label']);
        $lb = intval($b['label']);
        if ($la < $lb) return -1;
        if ($la > $lb) return 1;
        return 0;
    }
); 

从 PHP 7 开始,可以直接使用 spaceship 算子,

usort ($info['options'], function ($a,$b) {
        return intval($a['label']) <=> intval($b['label']);
    }
); 

从此更改您的usort实现的第一个参数...

usort($info['label'], function ($a,$b){
    return strcmp($a['label'],$b['label']);
}

对此...

usort($info['options'], function ($a,$b){
    return strcmp($a['label'],$b['label']);
}

您只是使用了错误级别的数组进行排序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM