There is a json file saved in a string like {"Org1": [{"a": 1, "b": 2}, {...} ...]} (file name is file.json)

More visual view:

{ "Org1": [ { "a": 1, "b": 2 }, { "a": 12, "b": 345 }, { "a": 12364, "b": -1 } ], "Org2": [ ] } 

You need to change "Org1" to "Org123", without changing the order. The values ​​"Org1" and "Org123" in the form of the variables "$ SchoolOldName" and "$ SchoolName" are correspondingly.

Problem:

 $file = "file.json"; $json = json_decode(file_get_contents("$file"), true); $json = str_replace($SchoolOldName, $SchoolName, $json); file_put_contents("$file", json_encode($json)); 

In this embodiment, nothing works.

If you do without json_decode / json_encode - just through file_get_contents, then everything works until "Org1" is written in Russian characters. Then, without json_encode, the name would be "\ u0410 \ u0432 \ u0430 \ u043b \ u043e \ u043d" and str_replace will not work.

Tell me, please, how to transcode json string into Russian text (so that it remains a string, not an array)? Or how to make a working version of the replacement values ​​using json_decode?

  • one
    str_replace - Alexey Shimansky
  • Why ask a new question that is 90% identical to the previous one? It was possible to edit the old. - br3t
  • @ Alexey Shimansky Thank you! Replaced $jsonLectures = preg_replace("/\"$SchoolOldName\"/m", "\"$SchoolName\"", $jsonLectures); on $jsonLectures = str_replace($SchoolOldName, $SchoolName, $jsonLectures); Now the json file does not break, but the Russian values ​​still do not change for some reason ... - Ya_O
  • @Ya_o what means do not change? Russian values ​​in json does not change to Russian value from a variable? Any errors? - Alexey Shimansky
  • @ Alexey Shimansky Yes. Russian values ​​in json do not change to Russian values ​​of the variable (this is if without json_encode / decode. If it is with them, it does not work at all). - Ya_O

0