简体   繁体   English

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

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

I'm working on a magento site php code and trying to sort array using label ASC.我正在研究 magento 站点 php 代码并尝试使用 label ASC 对数组进行排序。 so the values should be sort by label.所以这些值应该按 label 排序。

I used usort function for that我为此使用了usort function

but even i use function output can't see any differences.但即使我使用 function output 也看不出任何差异。

Ex-前任-

        //$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,
                    );

*/

Sort function is below排序 function 如下

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

even i sort or not array shows this output即使我排序或不数组显示这个 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"
        }
      }  
      
    }

I tried few other array keys too but non of them not worked, Anyone can help me to sort this, Thank You我也尝试了其他几个数组键,但没有一个不起作用,任何人都可以帮我排序,谢谢

Try converting them to integers because a string compare is very different from integer compare.尝试将它们转换为整数,因为字符串比较与 integer 比较非常不同。 For example, 2 comes lexicographically after than 100 , but that's not what you want.例如, 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;
    }
); 

As of PHP 7, you can directly use the spaceship operator,从 PHP 7 开始,可以直接使用 spaceship 算子,

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

change the first parameter of your usort implementation from this...从此更改您的usort实现的第一个参数...

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

To this...对此...

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

You were just using the wrong level of the array to sort on.您只是使用了错误级别的数组进行排序。

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

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