Task: There is a JSON file of this type:
{ "Postcode": 246050, "Region": "ГОМЕЛЬСКАЯ", "Area": "ГОМЕЛЬСКИЙ", "TownType": "Г", "TownName": "ГОМЕЛЬ", "StreetType": "УЛ", "StreetName": "БИЛЕЦКОГО", "Buildings": "4,5" }, { "Postcode": 246050, "Region": "ГОМЕЛЬСКАЯ", "Area": "ГОМЕЛЬСКИЙ", "TownType": "Г", "TownName": "ГОМЕЛЬ", "StreetType": "УЛ", "StreetName": "ВОЛОТОВСКАЯ", "Buildings": "2,2Г" }, It must be converted to another JSON so that the "Buildings" parameter is 1 value each. Those. like this:
{ "Postcode": 246050, "Region": "ГОМЕЛЬСКАЯ", "Area": "ГОМЕЛЬСКИЙ", "TownType": "Г", "TownName": "ГОМЕЛЬ", "StreetType": "УЛ", "StreetName": "БИЛЕЦКОГО", "Buildings": "4" }, { "Postcode": 246050, "Region": "ГОМЕЛЬСКАЯ", "Area": "ГОМЕЛЬСКИЙ", "TownType": "Г", "TownName": "ГОМЕЛЬ", "StreetType": "УЛ", "StreetName": "БИЛЕЦКОГО", "Buildings": "5" }, { "Postcode": 246050, "Region": "ГОМЕЛЬСКАЯ", "Area": "ГОМЕЛЬСКИЙ", "TownType": "Г", "TownName": "ГОМЕЛЬ", "StreetType": "УЛ", "StreetName": "ВОЛОТОВСКАЯ", "Buildings": "2" }, { "Postcode": 246050, "Region": "ГОМЕЛЬСКАЯ", "Area": "ГОМЕЛЬСКИЙ", "TownType": "Г", "TownName": "ГОМЕЛЬ", "StreetType": "УЛ", "StreetName": "ВОЛОТОВСКАЯ", "Buildings": "2Г" }, I wrote this code here:
$d1 = file_get_contents('1.json'); $js1 = json_decode($d1); $js2= array(); $I = count($js1); $s=0; for ($i=0; $i<$I; $i++){ $par = explode(",", $js1[$i]->Buildings); $J = count($par); for ($j=0; $j<$J; $j++){ array_push($js2, $js1[$i]); $js2[$s]->Buildings=$par[$j]; $s++; } } But why he breaks json as it should, but in the Buildings parameter writes only the last value from the string. Although if in a loop after $ s ++ ask
echo $par[$j]; echo "<br/>"; then it will show everything right, as it should go. But in the new array does not want to set different parameters.