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,But my Secend Row is Defference and I Get a Notice, How Can I Make a Condition For Type To Skip This Notice? When I Have Not type In my json This is My json in Row 1 and Is ok:

[{"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 json in Row 2 Without type and I Get Two ,,:

[{"id":"26","answer":[{"option":"3","text":"HIGH"}]},
{"id":"30","answer":[{"option":"3","text":"LOW"}]},
{"id":"31","answer":[{"option":"3","text":"LOW"}]]

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) {
            $type[] = $data['type'];
            }
            $types= implode(',',$type);
            $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);
?>

And This Is My Output: Notice: Undefined index: type in C:\wamp64\www\json\json.php on line 20 update user_survey_start set type2=',,' where us_id=256793

Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103
  • 1
    you have to check whether are you geting the $type array values or not ? `if(isset($types) && $types != '') { $types= implode(',',$type); } else { $types = ''; }` – prakash tank Feb 22 '17 at 06:30
  • I'm New In PHP Can You Correct My Code Please.. – Saeed Heidarizarei Feb 22 '17 at 06:31
  • 1
    You're error message has nothing to do with your query. You simply need to check if `$data['type']` exists before using it. Check out the php function [`array_key_exists()`](http://php.net/manual/en/function.array-key-exists.php) – M. Eriksson Feb 22 '17 at 06:33

1 Answers1

1

An "undefined index" messages appears when you're trying to use an array key that doesn't exist.

When using arrays where you don't know if the key exist or not, you need to check if it exists before trying to use it.

foreach ($jason_array as $data) {
    // If $data doesn't have a key called 'type', you'll get that notice.
    $type[] = $data['type'];
}

Let's use PHP's function array_key_exists() to check if the key exists:

foreach ($jason_array as $data) {
    if (array_key_exists('type', $data)) {
        // Now we will only use it if the key 'type' actually exists
        $type[] = $data['type'];
    }
}

Read more about PHP: “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset”

Community
  • 1
  • 1
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40