Insert parsed json data into MySQL table

93
January 25, 2019, at 01:10 AM

i already parsed multidimensional array. The last thing what i need to do: insert data into mysql table, another thing what i would like to know how to actually insert multidimensional array into table, should i use second $sql query and another table ? Below just example i used only three variable "id, main, icon" and of course need to pack all of them. Would appreciate your help. Thank you

 $jsonIterator = new RecursiveIteratorIterator(
  new RecursiveArrayIterator(json_decode($data, TRUE)),
  RecursiveIteratorIterator::SELF_FIRST);
  foreach($jsonIterator as $key=>$value)
   { 
   $query = "INSERT INTO json1 (id, main, icon) VALUES (
     '".$key["coord"]."', 
     '".$key["lon"]."', 
     '".$key["lat"]."'); "; 
   if(is_array($value))
     { echo "$key:\n"; }
   else
     { echo "$key => $value\n"; } 
}

Here is my json file

{
    "coord": {
        "lon": 22,
        "lat": 50.04
    },
    "weather": [{
        "id": 804,
        "main": "Clouds",
        "description": "overcast clouds",
        "icon": "04n"
    }],
    "base": "stations",
    "main": {
        "temp": 267.15,
        "pressure": 1007,
        "humidity": 79,
        "temp_min": 267.15,
        "temp_max": 267.15
    },
    "visibility": 10000,
    "wind": {
        "speed": 4.6,
        "deg": 50
    },
    "clouds": {
        "all": 90
    },
    "dt": 1548271800,
    "sys": {
        "type": 1,
        "id": 1711,
        "message": 0.0097,
        "country": "UK",
        "sunrise": 1548224204,
        "sunset": 1548256302
    },
    "id": 759734,
    "name": "London",
    "cod": 200
}
Answer 1

You seem to be making this much harder than it needs to be. There's no array in that JSON file, it's just a single object, so there's nothing to iterate over. You also don't need a recursive iterator.

$array = json_decode($data, true);
$id = $array['id'];
$lon = $array['coord']['lon'];
$lat = $array['coord']['lat'];
$query = "INSERT INTO json1 (id, main, icon) VALUES ($id, $lon, $lat)";
Rent Charter Buses Company
READ ALSO
New Laravel (Homestead) installation: 502 Bad Gateway - *Refresh* - the website is displayed correctly

New Laravel (Homestead) installation: 502 Bad Gateway - *Refresh* - the website is displayed correctly

I tried to setup a local Laravel projectSo I decided to use the Homestead vagrant box

146
Get last 15 lines from a large file in SFTP with phpseclib

Get last 15 lines from a large file in SFTP with phpseclib

I want to get the last 15 lines from a large file (30MB) from a SFTP server using PHP

134
Call to a member function pullCache() on null [on hold]

Call to a member function pullCache() on null [on hold]

I have problem with laravel application i dont know why is the problem, i create new laravel application with auth and when i try logout the user the i get the: "Call to a member function pullCache() on null"

124
how to limit number of pagination

how to limit number of pagination

I am creating pagination script but my pagination script display number and next and prev button only I have tried to create number of pagination limit but not display correct results

188