【问题标题】:how to read data from an arraylist in php [duplicate]如何从php中的arraylist读取数据[重复]
【发布时间】: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代码

标签: php arrays json arraylist


【解决方案1】:

你的错误是关于未定义的方法 您的类 $data 中没有名称为 fetch 的函数 重新检查你的 $data 类


请检查以下代码。 我使用你的 json 并访问所有数据 您可以像这样将数据作为数组处理

$str = '{"orderHistory":[{"product_name": "mike","product_price": 15000,"product_id": 5},{"product_name": "tv","product_price": 25000,"product_id": 3}]}';
$a = json_decode($str);
echo (json_encode($a->orderHistory));
echo ($a->orderHistory[0]->product_name);
echo ($a->orderHistory[1]->product_name);

【讨论】:

  • 你能告诉我如何使用 php 读取数据(json arraylist)并将其存储在变量中
  • 再次检查我的答案,伙计
猜你喜欢
  • 2017-06-08
  • 2012-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
  • 2013-07-05
  • 2018-01-08
  • 2020-01-12
相关资源
最近更新 更多