У меня есть 2 массива: цвета и любимые цвета.
Я хочу вывести массив со всеми цветами, но любимыми цветами сверху, сохраняя тот же порядок сортировки.
Мой пример работает нормально, но я хотел знать, является ли это правильным (самым быстрым) способом сделать это.
Спасибо
<code>$colors_arr = array("yellow","orange","red","green","blue","purple");
print "<pre>Colors: ";
print_r($colors_arr);
print "
";
$ favourite_colors_arr = array ("зеленый", "синий");
печать "
Favorite Colors: ";
print_r($favorite_colors_arr);
print "
";
$ normal_colors_arr = array_diff ($ colors_arr, $ favour_colors_arr);
печать "
Colors which are not favorites: ";
print_r($normal_colors_arr);
print "
";
// $ sorted_colors_arr = $ favour_colors_arr + $ normal_colors_arr;
$ sorted_colors_arr = array_merge ($ favour_colors_arr, $ normal_colors_arr);
печать "
All Colors with favorites first: ";
print_r($sorted_colors_arr);
print "
";
выход:
Colors: Array
(
[0] => yellow
[1] => orange
[2] => red
[3] => green
[4] => blue
[5] => purple
)
Favorite Colors: Array
(
[0] => green
[1] => blue
)
Colors which are not favorites: Array
(
[0] => yellow
[1] => orange
[2] => red
[5] => purple
)
All Colors with favorites first: Array
(
[0] => green
[1] => blue
[2] => yellow
[3] => orange
[4] => red
[5] => purple
)