1

When I am adding same unit of number in "num". eg(0-9). It is sorting my array. But if any of the "num" value contains a different unit of digits, the sorting fails. eg(4,7,2,1) this will work. (7,12,76,2) this will fail.

 $stack = array(array("Price" => $op,"num" => $noi),
        array("Price" => $op1,"num" => $noi1),
        array("Price" => $op2,"num" => $noi2),
        array("Price" => $op3,"num" => $noi3));



    function cmp($a, $b)
    {
      return strcmp($a["num"], $b["num"]);
    }
      usort($stack, "cmp");
Tanuj
  • 567
  • 6
  • 12
  • Why use a strcmp when comparing numbers? Why not just copy the first example in [php-manual](http://php.net/manual/en/function.usort.php) `if ( $a['num'] == $b['num'] ) return 0; else return $a['num'] < $b['num'] ? -1 : 1;` – Hasse Björk Nov 29 '15 at 21:28

1 Answers1

1

Try this for comparison of numbers

function cmp($a, $b) {
    if ($a['num'] == $b['num']) {
        return 0;
    }
    return ($a['num'] < $b['num']) ? -1 : 1;
}

It is taken straight out of the PHP manual on usort, and modified for your array.

Hasse Björk
  • 1,431
  • 13
  • 19