PHP multi-level array_merge. How to do?

Asked

Viewed 371 times

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 function merge with the 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 obtained:

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 don’t understand why the function merge is deleting the contents of the array [arrEnvSettings]. According to the documentation it should overwrite only the value [st3] going from st3.php for 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?

1 answer

2


Yes, the array_merge serves to unite two arrays, according to the indexes. If it is numerical, it joins the two arrays, reordering the indexes. If it is an index named, it will replace by array passed in the second parameter.

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

Example of array_merge_recursive. It will recursively join multidimensional indices:

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

array_merge_recursive($a, $b);

The result will be:

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

Note now the difference using the function array_replace_recursive. Unlike array_merge_recursive, it will replace according to the values of the indices, regardless of whether it is numerical or not.

 array_replace_recursive($a, $b);

  [
     "nomes" => [
       "Randrade",
       "Guilherme",
     ],
   ]
  • 2

    Could you add an example Wallace? More for curiosity purposes(my hehe). It can be in ideone or phpfidle same.

  • 1

    Yes, yes... I’m solving a stick here in the system, but I’ll post

  • worked with array_replace_recursive($arrIni,$setParam). The function array_merge_recursive($arrIni,$setParam) will return the old value and the new one. In other words, you will get the array [st3] and will insert an array of 2 results.

  • echo "<pre>";&#xA; print_r(array_replace_recursive($arrIni,$setParam));&#xA; echo "</pre>";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.