dongmao4486
2017-02-22 06:27
浏览 102

通过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

图片转代码服务由CSDN问答提供 功能建议

我正在解析这个json数组并且我想要 type 对象并将其放入 在New Column type2 中,这是My json Rows的一行,但我的Secend Row是Defference,我收到通知,我如何制作一个条件 对于类型要跳过此通知吗?当我没有<代码>键入在我的json 这是我的第1行中的json并且没问题:

 < 代码> [{ “ID”: “26”, “回答”:[{ “选项”: “3”, “文本”: “HIGH”}], “类型”: “3”},\ N {“ID  “:” 30" , “回答”:[{ “选项”: “3”, “文本”: “LOW”}], “类型”: “3”},\ N { “ID”: “31”,  “回答”:[{ “选项”: “3”, “文本”: “LOW”}], “类型”: “3”}] 
上  
 
 

这是我的json在第2行没有 type 而我得到两个,,,

  [{“id”:“26”,“answer  “:[{” 选项 “:” 3" , “文本”: “HIGH”}]},\ N { “ID”: “30”, “回答”:[{ “选项”: “3”,“文本 “:” LOW “}]},\ N {” ID “:” 31" , “回答”:[{ “选项”: “3”, “文本”: “LOW”}]] 
上   
 
 

这是我的代码:

 &lt;?php 
 $ con = mysqli_connec  t(“localhost”,“root”,“”,“array”); 
mysqli_set_charset($ con,“utf8”); 
 
 //检查连接
if(mysqli_connect_errno()){
 echo“失败 连接到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(',',$ 类型); 
 $ sql2 =“更新user_survey_start set type2 ='$ types'其中us_id =”。$ row [1]; //运行更新sql 
 echo $ sql2。“&lt; br&gt;”; 
 mysqli_query  ($ con,$ sql2); 
} 
} 
} 
mysqli_close($ con); 
?&gt; 
   
 
 

这是我的 输出: 通知:未定义索引:在第20行输入C:\ wamp64 \ www \ json \ json.php update user_survey_start set type2 =',,'其中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”

    打赏 评论

相关推荐 更多相似问题