PHP array_merge multi-level. How to make?

0

I have the following array :

        $arrIni["ENV"]="US";   
        $arrIni["sap_db_server"] = "192.xxx.x.xx";

        $arrIni["local_db_server"] = "localhost";
        $arrIni["local_db_username"] = "root";

        //Default settings
        $arrIni["arrEnvSettings"]["UserTypeID"]=4;       
        $arrIni["arrEnvSettings"]["LocalizationID"]=1;
        $arrIni["arrEnvSettings"]["LangLabels"] = array();
        $arrIni["arrEnvSettings"]["pages"]["st1"]="st1.php";
        $arrIni["arrEnvSettings"]["pages"]["st2"]="st2.php";
        $arrIni["arrEnvSettings"]["pages"]["st3"]="st3.php";

I'm using the merge function with array $setParam

$setParam["arrEnvSettings"]["pages"]["st3"]="st3_V2.php";

as follows:

echo "<pre>";
    print_r(array_merge($arrIni,$setParam));
echo "</pre>";

This is the result:

Array
(
    [ENV] => US
    [sap_db_server] => 192.xxx.x.xx
    [local_db_server] => localhost
    [local_db_username] => root
    [arrEnvSettings] => Array
        (
            [pages] => Array
                (
                    [st3] => st3_V2.php
                )

        )

)

This is the desired result:

Array
(
    [ENV] => US
    [sap_db_server] => 192.xxx.x.xx
    [local_db_server] => localhost
    [local_db_username] => root
    [arrEnvSettings] => Array
        (
            [UserTypeID] => 4
            [LocalizationID] => 1
            [LangLabels] => Array
                (
                )

            [pages] => Array
                (
                    [st1] => st1.php
                    [st2] => st2.php
                    [st3] => st3_V2.php
                )

        )

)

I do not understand why the merge function is deleting the contents of the [arrEnvSettings] array. According to the documentation it should only overwrite the [st3] value from st3.php to st3_V2.php . "...Se os *arrays* dados têm as mesmas chaves string, então o último valor para uma chave irá sobrescrever o valor anterior ..." .

What am I doing wrong?

    
asked by anonymous 09.05.2016 / 20:07

1 answer

2

Yes, array_merge serves to join two arrays , according to the indexes. If it is numeric, it joins the two arrays, rearranging the indexes. If it is a named index, it will replace with array passed in the second parameter.

For the operation you are performing, I recommend using the array_merge_recursive or array_replace_recursive functions.

Example of array_merge_recursive . It will recursively merge multidimensional indices:

$a = ['nomes' => ['Wallace', 'Miguel']];
$b = ['nomes' => ['Randrade', 'Guilherme']];

array_merge_recursive($a, $b);

The result will be:

  [
     "nomes" => [
       "Wallace",
       "Miguel",
       "Randrade",
       "Guilherme",
     ],
   ]

Now notice the difference using the array_replace_recursive function. Unlike array_merge_recursive , it will replace according to the index values, regardless of whether it is numeric or not.

 array_replace_recursive($a, $b);

  [
     "nomes" => [
       "Randrade",
       "Guilherme",
     ],
   ]
    
09.05.2016 / 20:45