ArrayHelper.php 773 B

12345678910111213141516171819202122232425262728
  1. <?php
  2. namespace app\utils;
  3. class ArrayHelper
  4. {
  5. public static function merge($a, $b)
  6. {
  7. $args = func_get_args();
  8. $res = array_shift($args);
  9. while (!empty($args)) {
  10. foreach ((array)array_shift($args) as $k => $v) {
  11. if (is_int($k)) {
  12. if (array_key_exists($k, $res)) {
  13. $res[] = $v;
  14. } else {
  15. $res[$k] = $v;
  16. }
  17. } elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
  18. $res[$k] = static::merge($res[$k], $v);
  19. } else {
  20. $res[$k] = $v;
  21. }
  22. }
  23. }
  24. return $res;
  25. }
  26. }