Last active
August 22, 2017 18:01
-
-
Save cagartner/f3333ef3f76786b36bcb63351d7f6252 to your computer and use it in GitHub Desktop.
Merge two array in one, used like a php solution for jquery $.extend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Merge two arrays | |
* @param array $arr1 | |
* @param array $arr2 | |
* | |
* @return array | |
*/ | |
function array_extend(array $arr1, array $arr2) | |
{ | |
if (empty($arr1)) { | |
return $arr2; | |
} else if (empty($arr2)) { | |
return $arr1; | |
} | |
foreach ($arr2 as $key => $value) { | |
if (is_int($key)) { | |
$arr1[] = $value; | |
} elseif (is_array($arr2[$key])) { | |
if (!isset($arr1[$key])) { | |
$arr1[$key] = array(); | |
} | |
if (is_int($key)) { | |
$arr1[] = array_extend($arr1[$key], $value); | |
} else { | |
$arr1[$key] = array_extend($arr1[$key], $value); | |
} | |
} else { | |
$arr1[$key] = $value; | |
} | |
} | |
return $arr1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment