sunaoka/hash-php

Merge one or more associative arrays recursively. This function does not append numeric indexes like array_merge_recursive() function.

v1.0.0 2023-08-24 03:14 UTC

This package is auto-updated.

Last update: 2024-10-23 08:37:46 UTC


README

Merge one or more associative arrays recursively

This function does not append numeric indexes like array_merge_recursive() function.

For example

array_merge_recursive()

$result = array_merge_recursive(['key' => 'old value'], ['key' => 'new value']);
print_r($result);
// => Array
// (
//     [key] => Array
//         (
//             [0] => old value
//             [1] => new value
//         )
//
// )

but, Hash::merge()

$result = Hash::merge(['key' => 'old value'], ['key' => 'new value']);
print_r($result);
// => Array
// (
//     [key] => new value
// )

Installation

composer require sunaoka/hash-php

Usage

use Sunaoka\Hash\Hash;

Hash::merge(['key' => 'old value'], ['key' => 'new value']);

// => Array
// (
//     [key] => new value
// )
use Sunaoka\Hash\Hash;

$array1 = ['a' => '1', 'b' => ['c' => '1', 'd' => '1', 'g' => ['h' => '1']]];
$array2 = ['a' => '2', 'b' => ['c' => '2', 'e' => '2', 'g' => ['h' => '2']]];
$array3 = ['a' => '3', 'b' => ['c' => '3', 'f' => '3', 'g' => ['h' => '3']]];

Hash::merge($array1, $array2, $array3);

// => Array
// (
//     [a] => 3
//     [b] => Array
//         (
//             [c] => 3
//             [d] => 1
//             [g] => Array
//                 (
//                     [h] => 3
//                 )
// 
//             [e] => 2
//             [f] => 3
//         )
// 
// )