proper use of usort
See also: Sorting a multidimensional array by value
your example is invalid
Expected first record as a result: 0 => '6'
Possible interpretation of odbyx-index == id-index
If the indices in odbyx match the indices in id ?
ie: $array['id'][3] should be sorted by $array['odbyx'][3]
If so, then the code you provide should either get 0 => '8' for the first index (odbyx 1 with a higher priority than 3), or 0 => '1' (3 above).
Possible interpretation of odbyx-index == id-value
If the index in odbyx matches id values in id ?
ie The value of $array['odbyx'][1] defines the sorting for $array['id'][6] = '1'
In this case, the result should be 0 => '2'
None of these possible interpretations even match the very first result in your example. The lesson here is the specification, i.e. Carefully identify and describe the specific conditions necessary to solve your problem, in stackoverflow or elsewhere.
Here is the place to run
Since the problem you are asking to solve is complex, poorly defined, requires a significant amount of coding and testing and has significant performance implications, I will leave you with this tidbit of solving one of the impossible interpretations above. Good luck.
Class SimpleSorter { private $orderBy; private $sortMe; public static function sortByIndexedOrderField(array $sortMe, array $byMe) { $sorter = new self($sortMe); return $sorter->applyIndexedOrder($byMe); } public function __construct(array $sortMe) { $this->sortMe = $sortMe; } public function applyIndexedOrder(array $byMe): array { $this->orderBy = $byMe; $keys = array_keys($this->sortMe); // sort, first by odbyx, then by value usort($keys, function($a,$b){ $odbyx = 0; if (array_key_exists($a, $this->orderBy) && array_key_exists($b, $this->orderBy)) { $odbyx = $this->orderBy[$b] <=> $this->orderBy[$a]; } if (0 !== $odbyx) { return $odbyx; } return $this->sortMe[$a] <=> $this->sortMe[$b]; }); // reorder by new key order $result = []; foreach($keys as $key) { $result[$key] = $this->sortMe[$key]; } return $result; } } $array = []; $array["id"] = [ 0 => '8', 1 => '7', 2 => '3', 3 => '6', 4 => '5', 5 => '2', 6 => '1', ]; $array["odbyx"] = [ 0 => 1, 1 => 2, 2 => 3, 3 => 2, 4 => 3, 5 => 3, 6 => 3, ]; $idsSorted = SimpleSorter::sortByIndexedOrderField($array["id"], $array["odbyx"]); print_r($idsSorted);