我收到此错误 错误:第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"
}]
}
]
答案 0 :(得分:1)
在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);
,然后按照您现在使用的单个字符串插入。
答案 1 :(得分:-1)
我认为这个错误是因为你传递了这些数组
$quecode[$i] = $data[$j]['ANSWERS'][$i]['ADCODE'];
$answer[$i] = $data[$j]['ANSWERS'][$i]['ANSWER'];
这里:
VALUES ('$drmobile', '$quecode', '$divcode', '$brdcode', '$prdcode',
'$answer', '$createdon');
所以应该通过这个:
VALUES ('$drmobile', '$quecode[$i]', '$divcode', '$brdcode', '$prdcode',
'$answer[$i]', '$createdon');