【问题标题】:How to print the array value inside the JSON array [closed]如何在 JSON 数组中打印数组值 [关闭]
【发布时间】:2021-12-26 10:19:08
【问题描述】:

我使用 API 调用 PHP 页面。这个 API 有这个 JSON 发布数据

{"product[]":["Layer Management System","Broiler Management System"]}

如何分别获取 product[0] 的数据。我用这个方法

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
echo  $data["product[]"];

但它没有醒来并显示下面的错误

<b>Notice</b>: Array to string conversion in
<b>/production/test_reg3.php</b> on line <b>8</b><br />
Array

还有一种方法是

  $data = json_decode(file_get_contents('php://input'), true);
    print_r($data);
    echo  $data["product[0]"];

它显示下面的错误

<b>Notice</b>: Undefined index: product[0] in
<b>/production/test_reg3.php</b> on line <b>8</b><br />

我需要将 product[] 值存储在任何变量中

【问题讨论】:

    标签: php arrays json


    【解决方案1】:

    应该是

    $data["product[]"][0]
    

    不要混淆作为 PHP 语法一部分的括号和作为属性名称一部分的括号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-19
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 2018-12-15
      • 2020-06-30
      相关资源
      最近更新 更多