skip to Main Content

I’m working on a magento site php code and trying to sort array using label ASC.
so the values should be sort by label.

I used usort function for that

but even i use function output can’t see any differences.

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

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

even i sort or not array shows this 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

2

Answers


  1. Try converting them to integers because a string compare is very different from integer compare. For example, 2 comes lexicographically after than 100, but that’s not what you want.

    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,

    usort ($info['options'], function ($a,$b) {
            return intval($a['label']) <=> intval($b['label']);
        }
    ); 
    
    Login or Signup to reply.
  2. change the first parameter of your usort implementation from this…

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search