src/ArrayLib.php
/**
     * Merge two assosciative arrays. The indexes will be in the order of the first array, then the second
     * @param Array $array1 - an assosciative array to merge
     * @param Array $array2 - an assosciative array to merge.
     * @param \Closure $clashHandler - callback function to return the value should keep, should we encounter
     *                                 the same index in the two arrays. This will take as params:
     *                                 [index], [array1 value], [array2 value]
     *          
     * @return array - combined assosciative array.
     */
Merge two assosciative arrays. The indexes will be in the order of the first array, then the second
@param Array $array1 - an assosciative array to merge
@param Array $array2 - an assosciative array to merge.
@param \Closure $clashHandler - callback function to return the value should keep, should we encounter
the same index in the two arrays. This will take as params:
[index], [array1 value], [array2 value]

@return array - combined assosciative array.
Merge two assosciative arrays . The indexes will be in the order of the first array then the second @param Array $array1 - an assosciative array to merge @param Array $array2 - an assosciative array to merge . @param \ Closure $clashHandler - callback function to return the value should keep should we encounter the same index in the two arrays . This will take as params : [ index ] [ array1 value ] [ array2 value ] @return array - combined assosciative array .
public static function assoc_array_merge(array $array1, array $array2, \Closure $clashHandler)
    {
        $result_array = array();
        foreach ($array1 as $index => $value)
        {
            if (isset($array2[$index]))
            {
                $value = $clashHandler($index, $array1[$index], $array2[$index]);
                unset($array2[$index]);
            }
            $result_array[$index] = $value;
            unset($array1[$index]);
        }
        foreach ($array2 as $index => $value)
        {
            $result_array[$index] = $value;
        }
        return $result_array;
    }
public static function assoc_array_merge ( array $ array1 , array $ array2 , \ Closure $ clashHandler ) { $ result_array = array ( ) ; foreach ( $ array1 as $ index => $ value ) { if ( isset ( $ array2 [ $ index ] ) ) { $ value = $ clashHandler ( $ index , $ array1 [ $ index ] , $ array2 [ $ index ] ) ; unset ( $ array2 [ $ index ] ) ; } $ result_array [ $ index ] = $ value ; unset ( $ array1 [ $ index ] ) ; } foreach ( $ array2 as $ index => $ value ) { $ result_array [ $ index ] = $ value ; } return $ result_array ; }
Merge two assosciative arrays.
Merge two assosciative arrays .
# override the value by running the clash handler callback to decide. # Array 2 should only have indexes that were not in array 1 as clashes were unset. # Thus we can just merge straight in.