It was necessary to sort the array according to the dictionary (the elements outside the dictionary should go after the sorted ones), used this option https://stackoverflow.com/a/22094707/5326802 . Sorting works, but items outside the dictionary go to the top of the list, although they should go down. What is the error of this method? And are there any other solutions to the problem?
This option has been used -
$weights = array_flip(array('112','111','132','100')); usort($data, function($x, $y) use($weights) { if (!isset($weights[$x['id']], $weights[$y['id']])) { // none of the ids have weight, so sort by bare id return $x['id'] - $y['id']; } else if (!isset($weights[$x['id']])) { // x does not have weight, put it last return 1; } else if (!isset($weights[$y['id']])) { // y does not have weight, put it last return -1; } // both have weights, use them return $weights[$x['id']] - $weights[$y['id']]; });
1
and-1
? - Arnial