Olá pessoal!
Estou iniciando com JSON, e tenho uma lista forma da seguinte forma:
"sentence_id": 1,
"text": "I hate #ThisPhoneCompany products, you'd have to torture me to get me to use #ThisPhone.",
"tones": [
{ "score": 0.591225, "tone_id": "anger", "tone_name": "Anger" },
{ "score": 0.560098, "tone_id": "analytical", "tone_name": "Analytical" },
{ "score": 0.645985, "tone_id": "confident", "tone_name": "Confident" }
] },
{ "sentence_id": 2,
"text": "The emojis in #ThisPhone are stupid.",
"tones": [
{ "score": 0.760538, "tone_id": "anger", "tone_name": "Anger" }
] },
Meu código para percorrer a lista é:
$jsonObj = json_decode($result, true);
foreach($jsonObj["sentences_tone"] as $value){
for($i = 0; $i < count($value['tones'][$i]['score']); $i++){
print_r ($value);
echo '<br/><br/>valor: '.$value['tones'][$i]['score'][$i];
echo '<br/>sentimento: '.$value['tones'][$i]['tone_name'].'<br/>'; };
echo '<br/>texto: '.$value['text']; };
O problema é que sempre que há mais de um "tones" (como o "sentence_id": 1, onde há 3 "tones"), aparece o erro Notice: Undefined offset: 1 in /Applications/XAMPP/xamppfiles/htdocs/toneAnalyzer/test.php on line 28, já o "sentence_id": 2, onde há 1 só, imprime correto, sem dar erro... Alguma luz?