我收到此错误
错误:第 37 行路径名中的数组到字符串转换
<?php
include('config.php');
// read json file
if($_SERVER['REQUEST_METHOD'] == "GET"){
$filename = 'employee.json';
$json_data = file_get_contents($filename);
// $json_data=$_POST['QUESTION'];
//convert json object to php associative array
$data = json_decode($json_data,true);
if (is_array($data) || is_object($data))
{
for($j=0;$j<sizeof($data);$j++){
//$jsonData[$j] = $data[$j]['DATA'];
//$jsonAnswers[$j] = $data[$j]['ANSWERS'];
$drmobile = $data[$j]['DATA']['DRMOBILE'];
$divcode = $data[$j]['DATA']['DIVCODE'];
$brdcode = $data[$j]['DATA']['BRDCODE'];
$prdcode = $data[$j]['DATA']['PRDCODE'];
$createdon = $data[$j]['DATA']['CREATEDON'];
// echo $drmobile." -- ";
for($i=0;$i<sizeof($data[$j]['ANSWERS']);$i++)
{
$quecode[$i] = $data[$j]['ANSWERS'][$i]['ADCODE'];
$answer[$i] = $data[$j]['ANSWERS'][$i]['ANSWER'];
// $quecodes=$quecode[$i];
// $answers=$answer[$i];
echo $quecode[$i]." <--> ".$answer[$i]."<br/>";
}
echo "<br/>-----------<br/>";
$sql="INSERT INTO U_ANSWERS(DRMOBILE,ADCODE,DIVCODE,BRDCODE,PRDCODE,ANSWERS,CREATEDON)
VALUES ('$drmobile', '$quecode', '$divcode', '$brdcode', '$prdcode', '$answer', '$createdon');";
$qur = mysql_query($sql);
if($qur){
$json = array("status" => 1, "msg" => "Done User added!");
}else{
$json = array("status" => 2, "msg" => "Already Submitted");
}
}
}
}else{
$json = array("status" => 0, "msg" => "Request method not accepted");
}
@mysql_close($conn);
/* Output header */
// header('Content-type: application/json');
echo json_encode($json);
//close connection
?>
示例 Json
[{
"DATA": {
"DRMOBILE": "8985180306",
"DIVCODE": 2,
"BRDCODE": 2,
"PRDCODE": 2,
"CREATEDON": "2017-08-10 11:38:22"
},
"ANSWERS": [{
"ADCODE": 1,
"ANSWER": "Excellent"
}, {
"ADCODE": 2,
"ANSWER": "Yes"
}, {
"ADCODE": 3,
"ANSWER": "No"
}, {
"ADCODE": 4,
"ANSWER": "Yes"
}, {
"ADCODE": 5,
"ANSWER": "Yes"
}, {
"ADCODE": 6,
"ANSWER": "No"
}, {
"ADCODE": 7,
"ANSWER": "2-4"
}, {
"ADCODE": 8,
"ANSWER": "15"
}]
},{
"DATA": {
"DRMOBILE": "8985180305",
"DIVCODE": 1,
"BRDCODE": 1,
"PRDCODE": 1,
"CREATEDON": "2017-08-10 11:38:22"
},
"ANSWERS": [{
"ADCODE": 1,
"ANSWER": "Excellent"
}, {
"ADCODE": 2,
"ANSWER": "Yes"
}, {
"ADCODE": 3,
"ANSWER": "No"
}, {
"ADCODE": 4,
"ANSWER": "Yes"
}, {
"ADCODE": 5,
"ANSWER": "Yes"
}, {
"ADCODE": 6,
"ANSWER": "No"
}, {
"ADCODE": 7,
"ANSWER": "2-4"
}, {
"ADCODE": 8,
"ANSWER": "15"
}]
},{
"DATA": {
"DRMOBILE": "8985180307",
"DIVCODE": 2,
"BRDCODE": 2,
"PRDCODE": 2,
"CREATEDON": "2017-08-10 11:38:22"
},
"ANSWERS": [{
"ADCODE": 1,
"ANSWER": "Excellent"
}, {
"ADCODE": 2,
"ANSWER": "Yes"
}, {
"ADCODE": 3,
"ANSWER": "No"
}, {
"ADCODE": 4,
"ANSWER": "Yes"
}, {
"ADCODE": 5,
"ANSWER": "Yes"
}, {
"ADCODE": 6,
"ANSWER": "No"
}, {
"ADCODE": 7,
"ANSWER": "2-4"
}, {
"ADCODE": 8,
"ANSWER": "15"
}]
}
]
<小时/>
最佳答案
在 for 循环中,它显示 $quecode
和$answer
是数组。
在 SQL 语句中,您将它们用作字符串变量。
$sql="INSERT INTO U_ANSWERS(DRMOBILE,ADCODE,DIVCODE,BRDCODE,PRDCODE,ANSWERS,CREATEDON)
VALUES ('$drmobile', '$quecode', '$divcode', '$brdcode', '$prdcode', '$answer', '$createdon');";
现在可能有两种可能的方法:
将此 SQL 语句也放入循环中,并插入所有单独的 这些数组中的值(
$quecode
和$answer
)将这些(
$quecode
和$answer
)数组的值组合成一个字符串(例如,逗号 分隔的字符串)使用implode
功能如下:implode(",", $quecode);
然后插入您现在使用的单个字符串。
关于php - 在php中解析Json时插入错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45607557/