Domanda

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
    ),
);
È stato utile?

Soluzione

Change array_merge() to array_merge_recursive().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top