<?php $array = [ ['type' => 'F'], ['type' => 'C'], ['Bar'], ['type' => 'B'], ['type' => 'H'], ['type' => 'D'], ['Foo'], ['type' => 'D'], ['A', 'B', 'C'], ['type' => 'F'], ['type' => 'Foo'], ['type' => 'Bar'], ['type' => 'A'], ['type' => 'E'], ['type' => 'Bar'], ]; $priorities = [ 'A' => 8, 'B' => 7, 'C' => 6, 'D' => 5, 'E' => 4, 'F' => 3, 'G' => 2, 'H' => 1, ]; function arraySortPriority(array &$array, $column, array $priorities):void { usort($array, function($a, $b) use ($column, $priorities) { $aFocus = $a[$column] ?? null; $bFocus = $b[$column] ?? null; return [array_key_exists($column, $b), $priorities[$bFocus] ?? 0, $aFocus] <=> [array_key_exists($column, $a), $priorities[$aFocus] ?? 0, $bFocus]; }); } arraySortPriority($array, 'type', $priorities); var_export($array);
You have javascript disabled. You will not be able to edit any code.