Yii 2 Response formats: RAW, HTML, JSON, JSONP, XML

If you want that your method return data in specific format in Yii2 then you can use this simple construction:
Example method return in JSON format:

use yii\web\Response;
//......
public function actionJson()
    {
        $something = true; // or you can set for test -> false;
        $return_json = ['status' => 'error'];
        if ($something == true)
        {
            $return_json = ['status' => 'success', 'message' => ' is successfully saved'];
        }
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        return $return_json;
    }

/*header results
Connection: Keep-Alive
Content-Length: 55
Content-Type : application/json; charset=UTF-8
*/

Continue reading Yii 2 Response formats: RAW, HTML, JSON, JSONP, XML

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]
Continue reading How to merge two JSON files in PHP?