简体   繁体   中英

How to search variable through a JSON Array in PHP

I have an API that returns this:

 [ { "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 } ]

I have a variable that gives me a form

 <?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>

I need to search the JSON for the data that I put in the form currently I have this

 $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; } }

but this error returns to me

Notice: Trying to get property 'id' of non-object in C:\xampp\htdocs\PHP_PLEX\index.php on line 33

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\PHP_PLEX\index.php on line 33

foreach($response_array->id as $item)

should be

foreach($response_array as $item)

Another way is to parse the json as associative array

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM