承接 rikudou/array-merge-recursive 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rikudou/array-merge-recursive

最新稳定版本:v1.0.3

Composer 安装命令:

composer require rikudou/array-merge-recursive

包简介

Replacement of built-in array_merge_recursive function that doesn't add values to array but replaces them

README 文档

README

Tests Code Quality Coverage Status Download

Installation

composer require rikudou/array-merge-recursive

Description

The php function array_merge_recursive behaves a little confusingly and not at all like array_merge.

Example of confusing behavior:

<?php $array1 = [ 'test' => 'test' ]; $array2 = [ 'test' => 'test2' ]; $result = array_merge_recursive($array1, $array2); // $result =  // array(1) { // 'test' => // array(2) { // [0] => // string(4) "test" // [1] => // string(5) "test2" // } // }

As you can see, the built in function doesn't replace the same keys but instead merges them together.

Compared to regular array_merge:

<?php $array1 = [ 'test' => 'test' ]; $array2 = [ 'test' => 'test2' ]; $result = array_merge($array1, $array2); // $result =  // array(1) { // 'test' => // string(5) "test2" // }

The array_merge replaces the values with whatever comes latest but doesn't work for deep array structures.

This library replaces the array_merge_recursive behavior to work like regular array_merge while maintaining the ability to merge deep arrays recursively.

Example:

<?php use function Rikudou\ArrayMergeRecursive\array_merge_recursive; $array1 = [ 'test' => 'test' ]; $array2 = [ 'test' => 'test2' ]; $result = array_merge_recursive($array1, $array2); // $result =  // array(1) { // 'test' => // string(5) "test2" // }

Deeper level array example

These two arrays are used in the following example:

<?php $array1 = [ 'test' => [ 'key1' => 'test', 'key2' => 'test', 'key3' => 'test' ] ]; $array2 = [ 'test' => [ 'key2' => 'test2', 'key4' => 'test2' ] ];

Result of built-in array_merge_recursive

array(1) { 'test' => array(4) { 'key1' => string(4) "test" 'key2' => array(2) { [0] => string(4) "test" [1] => string(5) "test2" } 'key3' => string(4) "test" 'key4' => string(5) "test2" } } 

Here you can see that key2 gets changed to array with both values added.

Result of built-in array_merge

array(1) { 'test' => array(2) { 'key2' => string(5) "test2" 'key4' => string(5) "test2" } } 

Since array_merge doesn't work recursively it completely replaces the test key with value from 2nd array.

Result of array_merge_recursive from this library

array(1) { 'test' => array(4) { 'key1' => string(4) "test" 'key2' => string(5) "test2" 'key3' => string(4) "test" 'key4' => string(5) "test2" } } 

This library correctly replaces the key2 with later value while keeping the whole tree.

统计信息

  • 总下载量: 122.88k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: WTFPL
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固