Search code examples
phparray-merge

array_merge does not work for 2 special json arrays


There are 2 json arrays and need to merge them but array_merge does not work
I've searched and found out the json arrays where could be merged truly with array_merge

Here are my json data available in separate files:

{
  "J": [
    {
      "a": "BAAB1",
      "b": "English"
    },
    {
      "a": "BAAB2",
      "b": "1"
    },
    {
      "a": "BAAB3",
      "b": "L"
    },
    {
      "a": "BAAB4",
      "b": "Test1 ..."
    },
    {
      "a": "BAAB5",
      "b": "Test2 ..."
    },
    {
      "a": "BAAB6",
      "b": "Test3 ..."
    }
  ]
}

And:

{
  "J": [
    {
      "a": "BAAR1",
      "b": "T1"
    },
    {
      "a": "BAAR2",
      "b": "1"
    },
    {
      "a": "BAAR3",
      "b": "111111"
    },
    {
      "a": "BAAR4",
      "b": "3"
    },
    {
      "a": "BAAR5",
      "b": "3"
    },
    {
      "a": "BAAR6",
      "b": "222222"
    }
  ]
}

I read the files from a path on the server and then use json_decode for each read file and then use array_merge
I think the name of my json arrays ("J") is creating the problem

Here's my code:

//
$mLanguageFileName = $mClass_GeneralFunctions_General->GetStringBetween($mReadFile,"^^^","^^^^");
$mLanguageFile_Address = $ServerRoot_base.'/LAN/'.$mLanguageFileName.'.json';                
$mClass_GeneralFunctions_FileFunctions = new GeneralFunctions_FileFunctions();
$mJSONFile_Language_C = $mClass_GeneralFunctions_FileFunctions->ReadFile($mLanguageFile_Address);
      
//
$mThemeFileName_M0 = $mClass_GeneralFunctions_General->GetStringBetween($mReadFile,"^^^^","^^^^^");
$mThemeFile_M0_Address = $ServerRoot_base.'/M0T/'.$mThemeFileName_M0.'/'.$mThemeFileName_M0.'.json';  
$mClass_GeneralFunctions_FileFunctions = new GeneralFunctions_FileFunctions();
$mJSONFile_M0_C = $mClass_GeneralFunctions_FileFunctions->ReadFile($mThemeFile_M0_Address);          

//        

$mJsonArr_M0_C = json_decode($mJSONFile_M0_C, true);
$mJsonArr_Language_C = json_decode($mJSONFile_Language_C, true);

echo "\n\nmerge ... ";

$L = sizeof($mJsonArr_M0_C["J"]);
for ($i = 0; $i< $L; $i++)
{
    echo "\n\nmJsonArr_M0_C i = {$i} ";
    echo "\nmJsonArr_M0_C a = {$mJsonArr_M0_C["J"][$i]["a"]} ";
    echo "\nmJsonArr_M0_C b = {$mJsonArr_M0_C["J"][$i]["b"]} ";
    
}

$L = sizeof($mJsonArr_Language_C["J"]);
for ($i = 0; $i< $L; $i++)
{
    echo "\n\nmJsonArr_Language_C i = {$i} ";
    echo "\nmJsonArr_Language_C a = {$mJsonArr_Language_C["J"][$i]["a"]} ";
    echo "\nmJsonArr_Language_C b = {$mJsonArr_Language_C["J"][$i]["b"]} ";
    
}

$mJsonArr_C_LanguageM0 = array_merge($mJsonArr_Language_C, $mJsonArr_M0_C);         

for ($i = 0; $i< sizeof($mJsonArr_C_LanguageM0["J"]); $i++)
{
    echo "\n\nmJsonArr_C_LanguageM0 i = {$i} ";
    echo "\nmJsonArr_C_LanguageM0 a = {$mJsonArr_C_LanguageM0["J"][$i]["a"]} ";
    echo "\nmJsonArr_C_LanguageM0 b = {$mJsonArr_C_LanguageM0["J"][$i]["b"]} ";
    
}

The data for each json array is truly echo'd but the merge data is just the second json array in array_merge ($mJsonArr_M0_C)

How can I solve the problem?


Solution

  • This is how array_merge is supposed to work: the documentation states:

    If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.

    And that is exactly the case with a key like "J": it is a string key.

    In the case you presented, array_merge_recursive does the job. For example:

    $a = json_decode('{ "J": [{ "a": "BAAB1", "b": "English" }] }', true);
    $b = json_decode('{ "J": [{ "a": "BAAR1", "b": "T1" }] }', true);
    $merged = array_merge_recursive($a, $b);