src/Utilities/Arrays.php
public static function sortByCustomKeysOrder(array $array, array $keysOrder)
    {
        /*
         * No elements?
         * Nothing to do
         */
        if (empty($array)) {
            return null;
        }

        $ordered = [];

        /*
         * 1st iteration:
         * Get elements in proper / required order
         */
        if (!empty($keysOrder)) {
            foreach ($keysOrder as $key) {
                if (isset($array[$key])) {
                    $ordered[$key] = $array[$key];
                    unset($array[$key]);
                }
            }
        }

        /*
         * 2nd iteration:
         * Get the rest of elements
         */
        if (!empty($array)) {
            foreach ($array as $key => $element) {
                $ordered[$key] = $element;
            }
        }

        return $ordered;
    }
public static function sortByCustomKeysOrder(array $array, array $keysOrder)
    {
        /*
         * No elements?
         * Nothing to do
         */
        if (empty($array)) {
            return null;
        }

        $ordered = [];

        /*
         * 1st iteration:
         * Get elements in proper / required order
         */
        if (!empty($keysOrder)) {
            foreach ($keysOrder as $key) {
                if (isset($array[$key])) {
                    $ordered[$key] = $array[$key];
                    unset($array[$key]);
                }
            }
        }

        /*
         * 2nd iteration:
         * Get the rest of elements
         */
        if (!empty($array)) {
            foreach ($array as $key => $element) {
                $ordered[$key] = $element;
            }
        }

        return $ordered;
    }
public static function sortByCustomKeysOrder ( array $ array , array $ keysOrder ) { /*
         * No elements?
         * Nothing to do
         */ if ( empty ( $ array ) ) { return null ; } $ ordered = [ ] ; /*
         * 1st iteration:
         * Get elements in proper / required order
         */ if ( ! empty ( $ keysOrder ) ) { foreach ( $ keysOrder as $ key ) { if ( isset ( $ array [ $ key ] ) ) { $ ordered [ $ key ] = $ array [ $ key ] ; unset ( $ array [ $ key ] ) ; } } } /*
         * 2nd iteration:
         * Get the rest of elements
         */ if ( ! empty ( $ array ) ) { foreach ( $ array as $ key => $ element ) { $ ordered [ $ key ] = $ element ; } } return $ ordered ; }
Sorts an array by keys given in second array as values.
Keys which are not in array with order are pushed after sorted elements.

Example:
- array to sort:
<code>
array(
'lorem' => array(
'ipsum'
),
'dolor' => array(
'sit',
'amet'
),
'neque' => 'neque'
)
</code>
- keys order:
<code>
array(
'dolor',
'lorem'
)
</code>
- the result:
<code>
array(
'dolor' => array(
'sit',
'amet'
),
'lorem' => array(
'ipsum'
),
'neque' => 'neque'        // <-- the rest, values of other keys
)
</code>

@param array $array     An array to sort
@param array $keysOrder An array with keys of the 1st argument in proper / required order
@return null|array
Sorts an array by keys given in second array as values . Keys which are not in array with order are pushed after sorted elements .