-2

I have an array in the following format - I want to be able to sort this so the highest price is first - i've tried array_multisort() but I'm not having any luck so far.

    $working[33] = array('id' => '33', 'price' => '250.00');
    $working[34] = array('id' => '34', 'price' => '277.88');

Using some function to sort by price the end product should look as follows:- as the 277 price is greater than 250

    $working[34] = array('id' => '34', 'price' => '277.88');
    $working[33] = array('id' => '33', 'price' => '250.00');
Zabs
  • 13,852
  • 45
  • 173
  • 297
  • 2
    possible duplicate of [How to sort an array of associative arrays by value of a given key in PHP](http://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php) – Mihai Matei Jul 26 '13 at 09:28
  • 1
    array_multisort($working, SORT_ASC); - this seems to work.. – Zabs Jul 26 '13 at 09:29
  • 1
    @Zabs then please remove the question. – 19greg96 Jul 26 '13 at 09:30
  • 1
    and you may want to convert your strings to numbers – Zim84 Jul 26 '13 at 09:30
  • I was wrong - it doesn't work. I jumped the gun abit :) – Zabs Jul 26 '13 at 09:31
  • 1
    possible duplicate of [How do I sort a multidimensional array in php](http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php) – Jon Jul 26 '13 at 09:31
  • 1
    Possible duplicate of: http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2 – Arda Jul 26 '13 at 09:33
  • duplicate: http://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php – s1x Oct 13 '14 at 12:56

6 Answers6

8

Sorting using usort :

usort($working, function($a, $b) {
    return $a['price'] < $b['price'] ? 1 : -1;
});
Benz
  • 2,317
  • 12
  • 19
2

use below function to get your problem resolve

    function multisort (&$array, $key) {
         $valsort=array();
         $ret=array();
         reset($array);
         foreach ($array as $ii => $va) {
            $valsort[$ii]=$va[$key];
         }
         asort($valsort);
         foreach ($valsort as $ii => $va) {
             $ret[$ii]=$array[$ii];
         }
         $array=$ret;
     }

multisort($multiarr,"order");
liyakat
  • 11,825
  • 2
  • 40
  • 46
2

You can do it with array_multisort

Examples on the documentation.

Xavier W.
  • 1,270
  • 3
  • 21
  • 47
1

Remove quotes OR use numeric sort.

String "24" is greater than "1834" because 2 is greater than 1.

My solution

$working[33] = array('id' => '33', 'price' => '250.00');
$working[34] = array('id' => '34', 'price' => '277.88');
$working[35] = array('id' => '35', 'price' => '1289.57');
$working[36] = array('id' => '36', 'price' => '15.42');

foreach ($working as $key => $row) {
    $price[$key] = $row['price'];
}

array_multisort($price, SORT_NUMERIC, $working);

var_dump($working);

Output

array(4) {
  [0]=>
  array(2) {
    ["id"]=>
    string(2) "36"
    ["price"]=>
    string(5) "15.42"
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(2) "33"
    ["price"]=>
    string(6) "250.00"
  }
  [2]=>
  array(2) {
    ["id"]=>
    string(2) "34"
    ["price"]=>
    string(6) "277.88"
  }
  [3]=>
  array(2) {
    ["id"]=>
    string(2) "35"
    ["price"]=>
    string(7) "1289.57"
  }
}
Macbric
  • 472
  • 4
  • 10
  • It is not necessary to convert the strings to numeric/floats. – Benz Jul 26 '13 at 09:44
  • Firstly, the comment was addressed to your original answer with straight `sort` which was totally out of place. Secondly, there is no `"24"` or `"1834"` here so I have no idea what you are talking about. Thirdly, the quotes cannot be removed because the array is not hardcoded (if it were, you could sort it in the source). And finally, even if they could be removed where is the code that would sort the array? – Jon Jul 26 '13 at 09:47
  • It was an example. I posted my solution. – Macbric Jul 26 '13 at 10:05
-1

See Sorting arrays in php to solve your problem

Naveen
  • 7,944
  • 12
  • 78
  • 165
-1
    usort($working, function ($a, $b) {
        return bccomp($a['price'], $b['price'], 2);
    });
Steven W
  • 1
  • 2
  • 1
    How is this materially different from @Benz answer? – SmrtGrunt Jun 04 '19 at 17:54
  • because his return is inaccurate. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. 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.https://www.php.net/manual/en/function.usort.php – Steven W Jun 06 '19 at 16:22
  • I must have misread their solution. It look to me like it only ever returned -1 or 1, depending on the result of the comparison function. – SmrtGrunt Jun 06 '19 at 17:51