antonio sanchez
antonio sanchez

Reputation: 133

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

Upvotes: 0

Views: 44

Answers (2)

Kurohige
Kurohige

Reputation: 1418

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'];
       }
}

Upvotes: 1

Salketer
Salketer

Reputation: 15711

foreach($response_array->id as $item)

should be

foreach($response_array as $item)

Upvotes: 2

Related Questions