简体   繁体   中英

How to Update the value in the json file without changing the file combination in PHP

I've started a short time working with php. I have already written a php code to update the json file, ut after run the codes, my string of json file format changed and all of the strings set in one line!!

I json file to have the following format in In several lines:

 { "Param": { "ID":"1", "status":"ok", "username":"Rose", "password":"edition12548", "Enable":"1" }, "PRO": { "ID":"End", "Finished":"True" } } 

and use this code for update Enable value:

$jsondata = file_get_contents("C:\List.json");
$array = json_decode($jsondata, true);
      $array[Param]["Enable"]="2";
    file_put_contents('C:\List2.json', json_encode($array));

but the new json file format changed and all strings set in one line:

{"Param":{"ID":"1","status":"ok","username":"Rose","password":"edition12548","Enable":"2"},"PRO":{"ID":"End","Finished":"True"}}

I want to make changes by keeping the format What are the ways?

If you want to "prettify" the output of the json_encode function you have to add a second parameter to the function that is the "options" parameter. In your case (if I understood correctly) an example of the code you have to use will be:

json_encode($array, JSON_PRETTY_PRINT);

If you want to learn more about other json_encode options you can read this PHP documentation page: https://www.php.net/manual/en/function.json-encode.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM