1

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 

    1 Answer 1

    3
    $answers = array(); foreach ($jason_array as $data) { foreach($data['answer'] as $ans){ $answers[] =$ans['text'] ; } } $answers= implode(',',$answers); 
    2
    • Just Updated First Row And in Secend row, I Have Fatal error: [] operator not supported for strings in C:\wamp64\www\json\json.php on line 17CommentedFeb 20, 2017 at 11:52
    • It's fixed now.CommentedFeb 20, 2017 at 12:00

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.