Merging an array but keeping values that weren't in the other array [duplicate]

StackOverflow https://stackoverflow.com/questions/23442102

  •  14-07-2023
  •  | 
  •  

Вопрос

What I'm trying to do is read an INI file with PHP and then merge the array with another array. For example:

$a1 = parse_ini_file("file1.ini",true);
$a2 = parse_ini_file("file2.ini",true);
$info = array_merge($a1,$a2);

Now here's my problem, if I merge the arrays: The sub-arrays will be completely replaced if it is written in the second ini file.

file1.ini

[BASIC]
title=Title
otherInfo=1
[SECONDSECTION]
thestuffs=3

file2.ini

[BASIC]
otherInfo=6

Now if I were to look at the merged data $info I would get something like this:

$info = array(
    "BASIC" => array(
        "otherInfo" => 6
    ),
    "SECONDSECTION" => array(
        "thestuffs" => 3
    ),
);

DESIRED RESULT:

$info = array(
    "BASIC" => array(
        "title" => "Title"
        "otherInfo" => 6
    ),
    "SECONDSECTION" => array(
        "thestuffs" => 3
    ),
);
Это было полезно?

Решение

Change array_merge() to array_merge_recursive().

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top