Tuesday, May 22, 2012

PHP Merge Multidimensional Arrays

function md_array_merge(){
    $arrays = func_get_args();
    $return = array();
    foreach($arrays as $array){
        foreach($array as $key=>$val){
            if(is_array($val) && isset($return[$key])){
                $return[$key] = md_array_merge($return[$key], $val);
            } else {
                $return[$key] = $val;
            }
        }
    }
    return $return;
}

I wrote the above function to merge 2 or more multidimensional arrays. Use is similar to array_merge().

Provided with Open Source license without warranty

1 comment:

  1. Great snippet! Saved me a lot of time. Thanks for sharing.

    ReplyDelete