# 自定义一个PHP函数，不使用array\_merge完成数组合并

**URL:** <https://www.shiqidu.com/t/topic/561>\
**Category:** 开发调优\
**Tags:** PHP\
**Created:** [2019 年3 月 2 日 10:27 UTC](https://www.shiqidu.com/t/topic/561 "2019-03-02T10:27:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![Eureka](https://www.shiqidu.com/user_avatar/www.shiqidu.com/eureka/32/5_2.png) [@Eureka](https://www.shiqidu.com/u/Eureka)\
发布日期： [2019 年3 月 2 日 10:27 UTC](https://www.shiqidu.com/t/topic/561/1 "2019-03-02T10:27:47Z")

</div>

一道面试题，实现比较简单

```php
// 编写一个类型array_merge的函数
function array_mer()
{
    $resultArr = [];
    $arrays = func_get_args();
    if (empty($arrays)) return [];
    foreach ($arrays as $array) {
        if (is_array($array)) {
            foreach ($array as $value) {
                $resultArr[] = $value;
            }
        }
    }

    return $resultArr;
}

var_dump(array_mer([1,3,4,5], [5,6,7,8,9]));
/*
array (size=9)
  0 => int 1
  1 => int 3
  2 => int 4
  3 => int 5
  4 => int 5
  5 => int 6
  6 => int 7
  7 => int 8
  8 => int 9
*/

```
