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?
Could you add an example Wallace? More for curiosity purposes(my hehe). It can be in ideone or phpfidle same.
– user28595
Yes, yes... I’m solving a stick here in the system, but I’ll post
– Wallace Maxters
worked with
array_replace_recursive($arrIni,$setParam)
. The functionarray_merge_recursive($arrIni,$setParam)
will return the old value and the new one. In other words, you will get thearray [st3]
and will insert an array of 2 results.– zwitterion
echo "<pre>";
 print_r(array_replace_recursive($arrIni,$setParam));
 echo "</pre>";
– zwitterion