How to merge two JSON files in PHP?

If you need merge information from two json files with same structure but with different values then you can use this overide recursive method

//Recursive method for merge 2 json files
function mergeJsonArrays($priority_json, $original_json_content)
    {
        foreach ($original_json_content as $org_content_key => $org_content_value) {
            if (!array_key_exists($org_content_key, $priority_json)) {
                $priority_json[$org_content_key] = $org_content_value;
            } elseif (!is_string($org_content_value)) {
                $priority_json[$org_content_key] =  mergeJsonArrays($priority_json[$org_content_key], $org_content_value);
            }
        }
        return $priority_json;
    }

 $jsonAssoc = true;
  $first_config_file_path = 'config_folder/first_json_file.json';
  $json_first = is_file($first_config_file_path) ? json_decode(file_get_contents($first_config_file_path), $jsonAssoc) : NULL;

  $second_config_file_path = 'config_folder/second_json_file.json';
  $json_second = is_file($second_config_file_path) ? json_decode(file_get_contents($second_config_file_path), $jsonAssoc) : NULL;

 $result_json = mergeJsonArrays($json_first, $json_second);

Example [config_folder/first_json_file.json]

{
    "menu": {        
        "title":  "Priority menu name"       
    },
    "menu_elements": {
        "first_level":
            {
                "title": "!!!CHANGED!!! First item",
                "home": "1"
            }
        ,
        "third_level": 
            {
                "title": " !!!CHANGED!!! Test third element item",
                "url": "#"                
            }       
    }
}

Example [config_folder/second_json_file.json]

{
    "menu": {        
        "title":  "Standard menu name"       
    },
    "menu_elements": {
        "first_level":
            {
                "title": "First item",
                "home": "0",
                "language": "EN",                
                "template_name": "Bootstrap"
            }
        ,
        "second_level": [
            {
                "title": "Second item",
                "url": "/second_item",
                "alias": "sitem",
                "type": "url"
            }            
        ],
        "third_level":
            {
                "url" : "/folder/3level",
				"title": "3dlevel", 
                "type": "alias",                
                "modules":
                    [
                        {
                            "module_name": "userinfo",
                            "module_position": "top-right"
                        }
                    ]
            }
    }
}

Result JSON:

Array
(
    [menu] => Array
        (
            [title] => Priority menu name
        )

    [menu_elements] => Array
        (
            [first_level] => Array
                (
                    [title] => !!!CHANGED!!! First item
                    [home] => 1
                    [language] => EN
                    [template_name] => Bootstrap
                )

            [third_level] => Array
                (
                    [title] =>  !!!CHANGED!!! Test third element item
                    [url] => #
                    [type] => alias
                    [modules] => Array
                        (
                            [0] => Array
                                (
                                    [module_name] => userinfo
                                    [module_position] => top-right
                                )

                        )

                )

            [second_level] => Array
                (
                    [0] => Array
                        (
                            [title] => Second item
                            [url] => /second_item
                            [alias] => sitem
                            [type] => url
                        )

                )

        )

)