Such a situation

$db = new PDO('mysql:host=localhost;dbname=333','root',''); $jsondata = file_get_contents('sob.json'); $data = json_decode($jsondata,true); $stmt = $db->prepare("insert into country values(?,?)"); foreach ($data as $row) { $stmt->bindParam(1, $row['id']); $stmt->bindParam(2, $row['name']); $stmt->execute(); } 

Records this json format. Normal

 [ { "id":"8192", "name":"Π‘Π²Π΅Ρ‚Π»Π°Π½Π°" }, { "id":"8193", "name":"Π‘Π΅Ρ€Π³Π΅ΠΉ" }, { "id":"8194", "name":"ЖСня" }, { "id":"8195", "name":"Π‘Π»Π°Π²Π°" }, { "id":"8196", "name":"ΠšΠΈΡ€ΠΈΠ»Π»" } ] 

But this is my dead end Help

 { "status":"ok", "data":[ { "id":"8192", "name":"Π‘Π²Π΅Ρ‚Π»Π°Π½Π°" "params":[ { "name":"Π’ΠΈΠΏ автомобиля", "value":"Π‘ ΠΏΡ€ΠΎΠ±Π΅Π³ΠΎΠΌ" }, { "name":"ΠŸΡ€ΠΎΠ±Π΅Π³, ΠΊΠΌ", "value":"170 000 - 179 999" } ] }, { "id":"8193", "name":"Π‘Π΅Ρ€Π³Π΅ΠΉ" "params":[ { "name":"Π’ΠΈΠΏ автомобиля", "value":"Π‘ ΠΏΡ€ΠΎΠ±Π΅Π³ΠΎΠΌ" }, { "name":"ΠŸΡ€ΠΎΠ±Π΅Π³, ΠΊΠΌ", "value":"170 000 - 179 999" } ] }, { "id":"8194", "name":"ЖСня" "params":[ { "name":"Π’ΠΈΠΏ автомобиля", "value":"Π‘ ΠΏΡ€ΠΎΠ±Π΅Π³ΠΎΠΌ" }, { "name":"ΠŸΡ€ΠΎΠ±Π΅Π³, ΠΊΠΌ", "value":"170 000 - 179 999" } ] } ] } 
  • What exactly baffles you? How to access the params values? If so, then like this: $ data-> data [0] -> params [0] -> name // Car Type - Firepro
  • As I understand it, you have no base structure yet. Offhand: PDO cannot bind an array to a value in a request, it needs something that can be converted to a string without any problems. You need to process $ data before forming the PDO statement so that you get a one-dimensional array. Judging by the fact that your entities have the "params" property (an array, which is why PDO does not know what to do with it), it is worth creating a separate table for the day - first write to one table id and name, then to another params.name and params.value, and the tables themselves are linked by id - therainycat
  • @Firepro, json_decode for the second file returns null . - Klym 9:41 pm
  • @MaksimKlimenko it is incorrect, everywhere after the "name": "value" there is no comma before params, which must be there to separate the two fields, so null is returned - Firepro
  • @Firepro, Yes, I already see, well, that's the problem of the author. - Klym 9:51 pm

1 answer 1

First, you have no valid JSON . I quote edited:

 { "status": "ok", "data": [ { "id": "8192", "name": "Π‘Π²Π΅Ρ‚Π»Π°Π½Π°", "params": [{ "name": "Π’ΠΈΠΏ автомобиля", "value": "Π‘ ΠΏΡ€ΠΎΠ±Π΅Π³ΠΎΠΌ" }, { "name": "ΠŸΡ€ΠΎΠ±Π΅Π³, ΠΊΠΌ", "value": "170 000 - 179 999" }] }, { "id": "8193", "name": "Π‘Π΅Ρ€Π³Π΅ΠΉ", "params": [{ "name": "Π’ΠΈΠΏ автомобиля", "value": "Π‘ ΠΏΡ€ΠΎΠ±Π΅Π³ΠΎΠΌ" }, { "name": "ΠŸΡ€ΠΎΠ±Π΅Π³, ΠΊΠΌ", "value": "170 000 - 179 999" } ] }, { "id": "8194", "name": "ЖСня", "params": [{ "name": "Π’ΠΈΠΏ автомобиля", "value": "Π‘ ΠΏΡ€ΠΎΠ±Π΅Π³ΠΎΠΌ" }, { "name": "ΠŸΡ€ΠΎΠ±Π΅Π³, ΠΊΠΌ", "value": "170 000 - 179 999" }] } ] } 

And the cycle will now look like this:

 foreach($data['data'] as $row) { $stmt->bindParam(1, $row['id']); $stmt->bindParam(2, $row['name']); $stmt->execute(); } 
  • Yes, thanks to these commas. - user218938 10:02 pm