1

Today I provided the answer of this question and I wrote a script, but I found out that something went wrong.

Here is the first script

<?php 
$array = array( 
            "0" => array (
               "id" => 1204,
               "custom_price" => 33.1500    
            ),
            
            "1" => array (
               "id" => 1199,
               "custom_price" => 15.83  
            ),
            
            "2" => array (
               "id" => 1176,
               "custom_price" => 16.83  
            )
         );

usort($array, function($a, $b) {
    return $a['custom_price'] - $b['custom_price'];
});
echo "<pre>";
print_r($array);

and its output is (also you can check output on sandbox)

<pre>Array
(
    [0] => Array
        (
            [id] => 1176
            [custom_price] => 16.83
        )

    [1] => Array
        (
            [id] => 1199
            [custom_price] => 15.83
        )

    [2] => Array
        (
            [id] => 1204
            [custom_price] => 33.15
        )

)

So, my desired output should be sort like (custom_price 15.83, 16.83, 33.15000) but the actual output is (custom_price 16.83,15.83,33.15000). you can see 15.83 is smallest from 16.83. the sorting result is wrong

So, when I change custom_price 15.83 to 14.83 then sorting output is correct

<pre>Array
(
    [0] => Array
        (
            [id] => 1199
            [custom_price] => 14.83
        )

    [1] => Array
        (
            [id] => 1176
            [custom_price] => 16.83
        )

    [2] => Array
        (
            [id] => 1204
            [custom_price] => 33.15
        )

)

you can see output on sandbox

I can't understand what's going on.. any idea about this ?

My Question is: I check each iteration but can't identify the problem. when custom_price is 15.83 then result is wrong. why?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
  • 1
    you need to use `-1` or `0` or `1` when sorting float values – Kevin Jun 01 '18 at 06:07
  • @Ghost when custom_price is 15.83 then result is wrong. other wise result is ok. i am refer this answer https://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value – Bilal Ahmed Jun 01 '18 at 06:08
  • You're problem is you're using '-' to compare rather than <, = or > this results in other consequences you get the desired result by changing 15.83 to 15.82 due to a rounding issue. See Nigel Ren's complete answer below. – TommyBs Jun 01 '18 at 06:20
  • @TommyBs Nigel Ren's answer is fine. i got my answer – Bilal Ahmed Jun 01 '18 at 06:23

4 Answers4

6

There is a warning in the PHP manual about the return values from the usort() compare function (at http://php.net/manual/en/function.usort.php#refsect1-function.usort-parameters)...

Caution Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

Also from PHP 7. you can use the spaceship operator <=> which returns 1, 0, -1 depending on the comparison of the two values...

usort($array, function($a, $b) {
    return $a['custom_price'] <=> $b['custom_price'];
});

echo "<pre>";
print_r($array);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
2

There is a complete example in the PHP manual about usort. Here's the modified version to solve your problem:

<?php
function cmp($a, $b)
{
    if ($a['custom_price'] == $b['custom_price']) {
        return 0;
    }
    return ($a['custom_price'] < $b['custom_price']) ? -1 : 1;
}
bimlas
  • 2,359
  • 1
  • 21
  • 29
1

Below code will solve your problem,

usort($array, function($a, $b) {
    if($a['custom_price']==$b['custom_price']) return 0;
    return $a['custom_price'] > $b['custom_price'] ? 1 : -1;
});
PPL
  • 6,357
  • 1
  • 11
  • 30
-1

Updated function

usort($array, function($a, $b) {
    return $a['custom_price'] > $b['custom_price'];
});
Jignesh Patel
  • 1,028
  • 6
  • 10