Php 我得到错误非法字符串偏移量';请求ID';
我使用API,它以json的形式在我的url中返回一些数据: 这是实际的url数据:Php 我得到错误非法字符串偏移量';请求ID';,php,json,html,multidimensional-array,Php,Json,Html,Multidimensional Array,我使用API,它以json的形式在我的url中返回一些数据: 这是实际的url数据: data={%22requestId%22%3A%22546b384ce51f469a2e8b4567%22%2C%22numbers%22%3A{%22917566559950%22%3A{%22date%22%3A%222014-11-18+17%3A45%3A59%22%2C%22status%22%3A1%2C%22desc%22%3A%22DELIVERED%22}}} 我的PHP代码用于在数据库中
data={%22requestId%22%3A%22546b384ce51f469a2e8b4567%22%2C%22numbers%22%3A{%22917566559950%22%3A{%22date%22%3A%222014-11-18+17%3A45%3A59%22%2C%22status%22%3A1%2C%22desc%22%3A%22DELIVERED%22}}}
我的PHP代码用于在数据库中插入数据,但出现错误:
$request = $_REQUEST["data"];
$jsonData = json_decode($request,true);
$link = mysqli_connect("127.0.0.1", "root", "", "table");
foreach($jsonData as $key => $value)
{
$requestID = $value['requestId'] ;
$userId = $value['userId'];
$senderId = $value['senderId'];
foreach($value['report'] as $key1 => $value1)
{
//detail description of report
$desc = $value1['desc'];
// status of each number
$status = $value1['status'];
// destination number
$receiver = $value1['number'];
//delivery report time
$date = $value1['date'];
$query = "INSERT Query for store record ";
mysqli_query($link, $query);
}
}
这里的问题是错误
警告:第11行的非法字符串偏移量'requestId'
警告:非法字符串偏移量“userId”。。。。。
请解决它….TLDR强>
You should remove the foreach($jsonData as ...) around your code.
您正在使用关联数组的关键点对其进行预处理:
{
"requestId":"546b384ce51f469a2e8b4567",
"numbers":{
"917566559950":{
"date":"2014-11-18 17:45:59",
"status":1,
"desc":"DELIVERED"
}
}
}
第一次迭代时,键为“requestId”,值字段为“546b384ce51f469a2e8b4567”。在第二轮你得到了“数字”和上面的数组
$requestID = $jsonData['requestId'];
var_dump($requestID); // the requestID
// foreaching in the assoc array inside the 'numbers'
foreach ($jsonData["numbers"] as $key => $value) {
// description
$desc = $value['desc'];
// status of each number
$status = $value['status'];
// date
$date = $value['date'];
var_dump($key); // the key with 917...
}
让我补充一点:1)如果您解码URL编码的JSON,将对您和您的问题的潜在回答者有所帮助。2) 您收到的错误消息告诉您正在尝试使用'requestID'作为字符串的索引。因此,$value必须是字符串。这将是你检查$value的价值以及原因的线索。