hi all, i have a problem with multiline json file that i wish to insert into an sql db. my problem arose as i went from one data set to two in the JSON file. the one data set was sucked from the json file to the sql db via PHP no problems at all, but at the moment i lack the programming skills to complete the nested loop as i am new to programming with JSON and jsonlines via PHP and the "foreach" statement. I red up on it and checked the forum here for similar issues. So here is the file the data sets are separated by ]}, and a newline:
[{"utc": ["22. September 2011, 01\n Uhr"], "name": [" Vorarlberg"], "temp": ["11.6", "11.6", "10.2", "11.3", "10.5", "9.7", "11.1", "9.4", "7.3", "9.2", "6.9", "9.1", "8.7", "6.9"], "humidity": ["97 %", "98 %", "96 %", "91 %", "95 %", "100 %", "87 %", "77 %", "99 %", "90 %", "87 %", "79 %", "90 %", "94 %"], "pressure": ["1021.5 ", " ", "1020.2 ", " ", "1021.1 ", " ", "1021.4 ", " ", "1021.4 ", " ", "1021.7 ", " ", "k.A. ", "1021.7 ", " ", "1022.6 ", " ", "k.A. ", "k.A. ", "k.A. ", "k.A. ", "k.A. "], "location": ["Bregenz", "Rohrspitz", "Dornbirn", "Fraxern", "Feldkirch", "Bludenz", "Sulzberg", "Alberschwende", "Schoppernau", "Schroecken", "Warth", "Langen am Arlberg", "Brand", "Gaschurn"], "height": ["440 ", "395 ", "407 ", "810 ", "436 ", "561 ", "1014 ", "715 ", "837 ", "1244 ", "1470 ", "1218 ", "1032 ", "973 "], "wind": ["Sued, 8 ", "Suedost, 5 ", "Suedost, 1 ", "Nordost, 4 ", "Suedost, 1 ", "Nord, 1 ", "Sued, 7 ", "Nord, 1 ", "Suedost, 3 ", "Ost, 3 ", "Ost, 2 ", "Nordost, 8 ", "Suedwest, 4 ", "Ost, 1 "]},
{"utc": ["22. September 2011, 01\n Uhr"], "name": [" Tirol"], "temp": ["8.7", "6.2", "5.9", "7.9", "5.0", "3.6", "7.0", "3.8", "8.5", "7.7", "7.3", "6.0", "3.3", "3.2", "2.6", "4.6", "4.4", "8.6", "9.1", "8.5", "6.6", "6.3", "4.9", "4.7", "8.0", "5.9", "9.1", "8.1", "9.7", "9.6", "7.2", "7.7", "7.3", "11.5", "7.4", "3.6", "9.0", "8.0"], "humidity": ["57 %", "90 %", "98 %", "99 %", "98 %", "90 %", "44 %", "97 %", "95 %", "99 %", "95 %", "95 %", "96 %", "43 %", "24 %", "69 %", "97 %", "97 %", "96 %", "88 %", "68 %", "92 %", "95 %", "96 %", "94 %", "96 %", "97 %", "92 %", "94 %", "74 %", "93 %", "89 %", "96 %", "88 %", "97 %", "97 %", "83 %", "88 %"], "pressure": ["k.A. ", "k.A. ", "k.A. ", "1022.4 ", " ", "k.A. ", "k.A. ", "k.A. ", "k.A. ", "1023.4 ", " ", "1023.9 ", " ", "1024.6 ", " ", "k.A. ", "k.A. ", "k.A. ", "k.A. ", "k.A. ", "k.A. ", "1023.1 ", " ", "1023.1 ", " ", "k.A. ", "k.A. ", "k.A. ", "k.A. ", "k.A. ", "1022.3 ", " ", "k.A. ", "1024.0 ", " ", "k.A. ", "1022.0 ", " ", "k.A. ", "1023.4 ", " ", "k.A. ", "1022.7 ", " ", "1021.2 ", " ", "k.A. ", "k.A. ", "k.A. ", "k.A. "], "location": ["Galzig", "St. Anton am Arlberg", "Holzgau", "Reutte", "Ehrwald", "Galtuer", "Ischgl Idalpe", "Nauders", "Landeck", "Imst", "Haiming", "Umhausen", "St. Leonhard Pitztal", "Pitztaler Gletscher", "Brunnenkogel", "Obergurgl", "Seefeld", "Innsbruck Flugh.", "Innsbruck Univ.", "Rinn", "Patscherkofel", "Neustift Milders", "Steinach am Brenner", "Brenner", "Mayrhofen", "Achenkirch", "Jenbach", "Alpbach", "Kufstein", "Hahnenkamm", "Kitzbuehel", "Hochfilzen", "Koessen", "Lienz", "Sillian", "St. Jakob Defereggen", "Virgen", "Kals"], "height": ["2090 ", "1300 ", "1110 ", "843 ", "980 ", "1577 ", "2319 ", "1341 ", "809 ", "773 ", "671 ", "1029 ", "1452 ", "2843 ", "3440 ", "1939 ", "1184 ", "585 ", "609 ", "924 ", "2252 ", "1004 ", "1032 ", "1369 ", "633 ", "908 ", "539 ", "927 ", "492 ", "1763 ", "748 ", "960 ", "589 ", "668 ", "1081 ", "1383 ", "1193 ", "1351 "], "wind": ["Nordwest, 1 ", "Suedwest, 4 ", "West, 3 ", "Sued, 3 ", "Nordwest, 1 ", "Sued, 1 ", "Ost, 7 ", "Suedost, 4 ", "West, 7 ", "Windstille", "West, 3 ", "Sued, 3 ", "Windstille", "West, 11 ", "Suedwest, 9 ", "Sued, 4 ", "Suedwest, 4 ", "West, 5 ", "Suedwest, 3 ", "Sued, 4 ", "Nordwest, 19 ", "Suedwest, 5 ", "Suedost, 6 ", "Nordost, 4 ", "West, 1 ", "Suedwest, 2 ", "Windstille", "Ost, 4 ", "Suedwest, 3 ", "West, 4 ", "Ost, 1 ", "Ost, 10 ", "Sued, 1 ", "Nordwest, 3 ", "West, 3 ", "West, 3 ", "Nordwest, 4 ", "West, 1 "]}]
and here is the code that worked up til the second data set:
$string = file_get_contents($handle);
$arr = json_decode($string, true);
#echo var_dump($arr);
//array instances specific to json items
$id = $arr["name"];
$id2 = $arr["location"];
$id3 = $arr["height"];
$id4 = $arr["utc"];
$id5 = $arr["temp"];
$id6 = $arr["humidity"];
$id7 = $arr["wind"];
$id8 = $arr["pressure"];
$s=0;
//Enumerating Array index
foreach($arr as $item=> $value){
$s=count($value); // WIN
}
echo $s;
//suck the array for loop
for($i=0;$i<$s;$i++){
$name = $id[0];
$location = $id2[$i];
$height = $id3[$i];
$utc = $id4[0];
$temp = $id5[$i];
$humidity = $id6[$i];
$wind = $id7[$i];
$pressure = $id8[$i];
mysql_query("INSERT INTO wetteroe (name, location, height, utc, temp, humidity, wind, pressure, DATEtime) VALUES('$name', '$location', '$height', '$utc', '$temp', '$humidity', '$wind', '$pressure',current_timestamp())") or die (mysql_error());
}
ANY help on this would be appreciated!
Thanks,
Herb