0

I'm Parsing This json Array and I Want to Take type Object and Put That in New Column type2, and This is one Row of My json Rows, Why I Get This Warning for Some Rows? Warning: Invalid argument supplied for foreach() in C:\wamp64\www\json\json.php on line 18

[{"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","","array");
mysqli_set_charset($con,"utf8");

// Check connection
if (mysqli_connect_errno()){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

$sql="SELECT `survey_answers`,us_id FROM `user_survey_start`";
if ($result=mysqli_query($con,$sql)){
    while ($row = mysqli_fetch_row($result)){
        $json = $row[0];
        if(!is_null($json)){
            $jason_array = json_decode($json,true);
            // type2
            $type = array();
            foreach ($jason_array as $data) {
                if (array_key_exists('type', $data)) {
                    // Now we will only use it if it actually exists
                    $type[] = $data['type'];
                }
            }         
            // lets check first your $types variable has value or not?
            if(!empty($type)) {
             $types= implode(',',$type); /// implode yes if you got values
            } 
            else { 
                $types = ''; //blank if not have any values
            }
            $sql2="update user_survey_start set type2='$types' where us_id=".$row[1];//run update sql
            echo $sql2."<br>";
            mysqli_query($con,$sql2);
        }
    }
}
mysqli_close($con);
?>

That is Strang, Why Some Row Has Output And Some Rows Hasn't Any Output, Those Json Type Are Same. I Find The Problem, Because Some json entered, I Mean. This One Has Warning: Invalid argument supplied for foreach()

[{"id":"26","answer":[{"option":"4","text":"Hello
"}],"type":"3"}]

And This One is Okey

[{"id":"26","answer":[{"option":"4","text":"Hello"}],"type":"3"}]

How Can I Fix The Problem?

1 Answer 1

1

you also try is_array before your for each loop

if (is_array($jason_array))
{
     foreach ($jason_array as $data) {
    {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Yessss,Solved, Appriciate It.
I Haven't Any Error But in Output I Haven't Value For Those Rows Type Too, I Mean , I Have update user_survey_start set type2='' where us_id=267593
i think your variable $jason_array is not a array that'why not come any output
I Don't Know Why, Because My Json Type is Like Above, Two Same Row but Defference Value, One Retrun Output and Other One Have Invalid argument supplied
print your varibale $jason_array first then check what is return
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.