Hello, there is no such array in any way I can sort it out so that the qty and qtr keys are at the bottom depending on the smallest value, and qtr should be lower than qty

I tried it like this, but something does not sort correctly

usort($this->images, function($a, $b) { return ($a['qty'] >= $b['qty']) ? -1 : 1; }); usort($this->images, function($a, $b) { return ($a['qtr'] >= $b['qtr']) ? 1 : -1; }); 
  • @dogmar - please give an example of how the array should look after "sorting". - Opalosolo
  • Updated the link with the array - dogmar
  • @dogmar - Why are 4 above 1, and in the same situation where qtr are equal, are elements 0 and 3 in a different order? - Opalosolo
  • @ ua6xh 4 is above 1, because it has more qty, elements 0 and 3, I adjusted, it was not right, the head boiled from these sorts :) - dogmar
  • @dogmar - why, before sorting, element 2 is one: 2 => array ('name' => 'Test 3', 'qty' => '5', 'qtr' => '1',), and after sorting another: 2 => array ('name' => 'Test 3', 'qty' => '5', 'qtr' => '3',),? - Opalosolo

1 answer 1

  function cmp($a, $b){ if ($a['qtr'] > $b['qtr']) { return -1; } elseif(($a['qtr'] == $b['qtr'])) { if(($a['qty'] > $b['qty'])) { return 1; } else { return -1; } } else{ return 1; } } 

Ideone.com