Как искать переменную через массив JSON в PHP - PullRequest
0 голосов
/ 28 апреля 2020

У меня есть 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

Ответы [ 2 ]

2 голосов
/ 28 апреля 2020
foreach($response_array->id as $item)

должно быть

foreach($response_array as $item)
1 голос
/ 28 апреля 2020

Другой способ - проанализировать json как ассоциативный массив

$response_array = json_decode($response,true); # the 'true' makes associative array
foreach($response_array as $item)
{
       if($item['id']== $direccion)
       {
           echo $item['priority'];
       }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...