【问题标题】:How to search variable through a JSON Array in PHP如何在 PHP 中通过 JSON 数组搜索变量
【发布时间】:2020-04-28 14:51:16
【问题描述】:

我有一个返回这个的 API:

[
{
    "id": "5a162db4-4443-4ee8-828a-372562230bf9",
    "supplyItemNumber": "PDH2OS001",
    "type": "Machine",
    "category": "Coolants & Lubricants",
    "priority": "Alto",
    "group": "Lubricants",
    "description": "",
    "customerUnitPrice": 0.00,
    "inventoryUnit": "Litre",
    "briefDescription": "",
    "accountId": null,
    "manufacturerCode": null,
    "supplierId": null,
    "manufacturerItemNumber": "",
    "manufacturerItemRevision": "",
    "manufacturerText": "",
    "createdDate": "2018-12-11T22:57:58Z",
    "createdById": "6e6a5c41-f62c-460b-96f4-fcab58381bbe",
    "modifiedDate": "2019-03-06T15:46:56Z",
    "modifiedById": "6e6a5c41-f62c-460b-96f4-fcab58381bbe",
    "taxCodeNumber": null,
    "maxQuantity": 0.00,
    "minQuantity": 0.00
},
{
    "id": "96d85b76-5231-4ce4-95fc-7fa9c915caab",
    "supplyItemNumber": "PDH2OS002",
    "type": "Machine",
    "category": "Coolants & Lubricants",
    "priority": "Alto",
    "group": "Lubricants",
    "description": "",
    "customerUnitPrice": 0.00,
    "inventoryUnit": "Litre",
    "briefDescription": "",
    "accountId": null,
    "manufacturerCode": null,
    "supplierId": null,
    "manufacturerItemNumber": "",
    "manufacturerItemRevision": "",
    "manufacturerText": "",
    "createdDate": "2018-12-11T22:57:58Z",
    "createdById": "6e6a5c41-f62c-460b-96f4-fcab58381bbe",
    "modifiedDate": "2019-03-06T15:46:45Z",
    "modifiedById": "6e6a5c41-f62c-460b-96f4-fcab58381bbe",
    "taxCodeNumber": null,
    "maxQuantity": 0.00,
    "minQuantity": 0.00
} ]

我有一个变量给我一个表格

<?PHP


if (isset($_GET['direccion']))
{
  $direccion = $_GET['direccion'];
  echo 'Se ha ingresado 1 direccion:' . $direccion;
}

?>

<body>
        <form action="" method="GET">
            <label for="direccion">Ingresar Direccion:</label>
            <input type="text" name="direccion">
            
            <button type="submit">Consultar</button>
            
        </form>
    </body>

我需要在 JSON 中搜索我在表单中输入的数据 目前我有这个

$response = curl_exec($curl);
curl_close($curl);
$response_array = json_decode($response);
//$supplyItemNumber = array_search($direccion, array_column($response_array, 'supplyItemNumber'));    
 foreach($response_array->id as $item)
{
       if($item->id == $direccion)
       {
           echo $item->priority;
       }
}

但是这个错误返回给我

注意:试图获取非对象的属性“id” C:\xampp\htdocs\PHP_PLEX\index.php 在第 33 行

警告:为 foreach() 提供的参数无效 C:\xampp\htdocs\PHP_PLEX\index.php 在第 33 行

【问题讨论】:

    标签: php arrays json api


    【解决方案1】:
    foreach($response_array->id as $item)
    

    应该是

    foreach($response_array as $item)
    

    【讨论】:

      【解决方案2】:

      另一种方法是将json解析为关联数组

      $response_array = json_decode($response,true); # the 'true' makes associative array
      foreach($response_array as $item)
      {
             if($item['id']== $direccion)
             {
                 echo $item['priority'];
             }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-10-22
        • 2016-05-23
        • 1970-01-01
        • 1970-01-01
        • 2016-01-15
        • 2018-04-20
        • 2021-02-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多