【发布时间】:2018-05-13 11:59:06
【问题描述】:
我有一个 JSON 数组列表,需要使用 php 从中读取数据。我已经尝试过了,但失败了。我将在下面给出所有内容
输入数据:JSON
{
"orderHistory":[
{
"product_name": "mike",
"product_price": 15000,
"product_id": 5
},
{
"product_name": "tv",
"product_price": 25000,
"product_id": 3
}
]
}
PHP代码
// get posted data
$data = json_decode(file_get_contents("php://input"));
while ($row = $data->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo '{';
echo '"message": "array added."';
echo '}';
}
错误:
<b>Fatal error</b>: Uncaught Error: Call to undefined method stdClass::fetch() in
【问题讨论】:
-
->fetch(PDO::FETCH_ASSOC)与数据库查询一起使用。它不适用于已经解码的数组。 -
请告诉我读取数据的php代码