Skip to content

Instantly share code, notes, and snippets.

@andxbes
Created May 7, 2024 09:09
Show Gist options
  • Save andxbes/f60b730da0159b25963c1008d54585e0 to your computer and use it in GitHub Desktop.
Save andxbes/f60b730da0159b25963c1008d54585e0 to your computer and use it in GitHub Desktop.
Сортировка ассоциативного массива в порядке указанном массивом ключей.
<?php
if (!function_exists('array_sort_by_keys_array')) {
/**
* Сортировка ассоциативного массива в порядке указанном массивом ключей.
*
* @param array $array Входной массив.
* @param array $sorter Массив ключей в нужном порядке.
*
* @return bool
*/
function array_sort_by_keys_array(array &$array, array $sorter)
{
$sorter = array_intersect($sorter, array_keys($array));
$array = array_merge(array_flip($sorter), $array);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment