I'm Parsing my json and I Want to Update my awnser2
Column With text
Object of answer
Array, But my Output Is Last one of Option
and text
I Want Just text Object, Can you Help Me To Take That? This Is My json:
[{"id":"26","answer":[{"option":"3","text":"HIGH"}],"type":"3"}, {"id":"30","answer":[{"option":"3","text":"LOW"}],"type":"3"}, {"id":"31","answer":[{"option":"3","text":"LOW"}],"type":"3"}]
And This is My Code:
<?php $con=mysqli_connect("localhost","root","","arrayy"); // Check connection if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql="SELECT `survey_answers`,id_s FROM `user_survey_start`"; if ($result=mysqli_query($con,$sql)){ while ($row = mysqli_fetch_row($result)){ $json = $row[0]; $jason_array = json_decode($json,true); // awnser2 $answer = array(); foreach ($jason_array as $data) { foreach($data['answer'] as $ans){ echo $ans['text']."\n" ; } } $answers= implode(',',$ans); $sql3="update user_survey_start set awnser2='$answers' where id_s=".$row[1];//run update sql echo $sql3."<br>"; mysqli_query($con,$sql3); } } mysqli_close($con); ?>
And This Is My Output:
update user_survey_start set awnser2='3,HIGH' where id_s=1
But I Want to Have This One:
update user_survey_start set awnser2='HIGH,LOW,LOW' where id_s=1