dongmao4486 2017-02-22 06:27
浏览 142
已采纳

通过PHP解析不完整字符串的json

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

  • 写回答

1条回答 默认 最新

  • dqcwl02022 2017-02-22 06:49
    关注

    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”

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)