Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP解决JSON中的新行_Php_Mysql_Json - Fatal编程技术网

通过PHP解决JSON中的新行

通过PHP解决JSON中的新行,php,mysql,json,Php,Mysql,Json,我正在解析这个json数组,我想获取type Object并将其放入新的列type2中,这是我的json行中的一行, 我得到的foreach参数无效,因为json中的某些行中出现了新行。我怎样才能解决这个问题 这个不行 [{"id":"26","answer":[{"option":"4","text":"Hello "}],"type":"3"}] 这个没问题 [{"id":"26","answer":[{"option":"4","text":"Hello"}],"type":"3"}]

我正在解析这个json数组,我想获取type Object并将其放入新的列type2中,这是我的json行中的一行, 我得到的foreach参数无效,因为json中的某些行中出现了新行。我怎样才能解决这个问题

这个不行

[{"id":"26","answer":[{"option":"4","text":"Hello
"}],"type":"3"}]
这个没问题

[{"id":"26","answer":[{"option":"4","text":"Hello"}],"type":"3"}]
这是我的代码:

<?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);
?>

问题是JSON格式无效

如果文本内容有多行,则应使用\n,而不是键入enter

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

问题是JSON格式无效

如果文本内容有多行,则应使用\n,而不是键入enter

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

在json解码之前,将新行替换为\n:

$json = preg_replace('/\r|\n/','\n',trim($json));

$jason_array = json_decode($json,true);

在json解码之前,将新行替换为\n:

$json = preg_replace('/\r|\n/','\n',trim($json));

$jason_array = json_decode($json,true);

这个json是对问题和用户输入的问题的描述。“我有太多的行了,我该怎么办?”塞德里奇·阿里扎雷说,就拿大写字母来说吧。如果您需要批量修复数据,是否可以手动编辑这些数据,或者这些数据是否太多?是否存在错误模式?@SedricHeidarizarei在存储记录或执行json_decode之前将所有EOF字符替换为\n。此json描述了用户输入的问题。“我有太多的行了,我该怎么办?”塞德里奇·阿里扎雷说,就拿大写字母来说吧。如果您需要批量修复数据,是否可以手动编辑这些数据,或者这些数据是否太多?错误是否存在模式?@SedricHeidarizarei在存储记录或执行json_decode之前将所有EOF字符替换为\n。警告:使用mysqli时,应使用and将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。切勿将$\u POST或$\u GET数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。警告:在使用mysqli时,您应该使用并将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。千万不要将$\u POST或$\u GET数据直接放入查询,如果有人试图利用你的错误,这可能是非常有害的。解决了,你是一个糟糕的人,赞赏它解决了,你是一个糟糕的人,赞赏它