Gasperro
Gasperro

Reputation: 25

how to access array in array when inserting json data to database?

I have json file that contains of 50 lines of similar

"results":[{"score":0,"team_id":126266},{"score":0,"team_id":125798}].

I need to insert this to database, but I keep getting errors:

Notice: Undefined index: results[0]:score[0] in D:\xampp\htdocs\json\index.php on line 26

Notice: Undefined index: results[1]:score[0] in D:\xampp\htdocs\json\index.php on line 27

Notice: Undefined index: results[0]:team_id[0] in D:\xampp\htdocs\json\index.php on line 32

Notice: Undefined index: results[1]:team_id[0] in D:\xampp\htdocs\json\index.php on line 33

Here's the code:

$pirmo_oponento_rezultatas = $row['results[0]:score[0]'];
$antro_oponento_rezultatas = $row['results[1]:score[0]'];    
$fk_Komandosid_Komandos = $row['results[0]:team_id[0]'];
$fk_Komandosid_Komandos1 = $row['results[1]:team_id[0]'];

$sql="INSERT INTO rungtynes (pirmo_oponento_rezultatas,antro_oponento_rezultatas,fk_Komandosid_Komandos,
fk_Komandosid_Komandos1) VALUES ('$pirmo_oponento_rezultatas','$antro_oponento_rezultatas',
'$fk_Komandosid_Komandos','$fk_Komandosid_Komandos1')";

Upvotes: 0

Views: 38

Answers (1)

Devsi Odedra
Devsi Odedra

Reputation: 5322

You can access as below

$pirmo_oponento_rezultatas = $row['results'][0]['score']; 
$antro_oponento_rezultatas = $row['results'][1]['score'];

Upvotes: 1

Related Questions