简体   繁体   中英

PHP JSON getting specific value of each key

I have a php code that reads JSON files. Part of the JSON sample below:

 "Main": [{
    "count": 7,
    "coordinates": [89,77],
    "description": "Office",
  },{
    "count": 8,
    "coordinates": [123,111],
    "description": "Warehouse",
  }]

and I am trying to code PHP to only get the info (count, coordinates, description) of those who's description is included in the criteria like Warehouse . PHP Sample below

$validcriteria = array("Warehouse", "Parking_lot");

How do I do an if-statement to check first if "description" is included in the valid criteria. I tried the code below but it doesn't seem to work right.

$JSONFile = json_decode($uploadedJSONFile, false);
foreach ($JSONFile as $key => $value)
{
    if (in_array($key['description'] , $validcriteria))
    {
        #more codes here
    }
}

My code in PHP has been working except when I try to add $key['description'] to try and check the description first if it's valid. My code above is reconstructed to remove sensitive information but I hope you got some idea of what I was trying to do.

When attempting to understand the structure of a parsed JSON string, start with a print_r($JSONFile); to examine its contents. In your case, you will find that there is an outer key 'Main' which holds an array of sub-arrays. You will need to iterate over that outer array.

// Set $assoc to TRUE rather than FALSE
// otherwise, you'll get an object back
$JSONFile = json_decode($uploadedJSONFile, TRUE);
foreach ($JSONFile['Main'] as $value)
{
  // The sub-array $value is where to look for the 'description' key
  if (in_array($value['description'], $validcriteria))
  {
    // Do what you need to with it...
  }
}

Note: if you prefer to continue setting the $assoc parameter to false in json_decode() , examine the structure to understand how the objects lay out, and use the -> operator instead of array keys.

$JSONFile = json_decode($uploadedJSONFile, FALSE);
foreach ($JSONFile->Main as $value)
{
  // The sub-array $value is where to look for the 'description' key
  if (in_array($value->description, $validcriteria))
  {
    // Do what you need to with it...
  }
}

You might also consider using array_filter() to do the test:

$included_subarrays = array_filter($JSONFile['Main'], function($a) use ($validcriteria) {
  return in_array($a['description'], $validcriteria);
});
// $included_subarrays is now an array of only those from the JSON structure
// which were in $validcriteria

Given your JSON structure, you probably want

foreach($decoded_json['Main'] as $sub_array) {
    if (in_array($sub_array['description'], $validation)) {
       ... it's there ...
    }
}

Because you set false to second argument of json_decode function, it's returned as an object, if you change it to TRUE, the code would work.

http://php.net/manual/en/function.json-decode.php

and you have to change key to value;

foreach ($JSONFile->Main as $key => $value)
{
    if (in_array($value->description, $validcriteria))
    {
        #more codes here

    }
}

this code assume your json file has one more depth greater than your example. that's why $JSONFile->Main in the foreach loop.

尝试使用:

if ( array_key_exists('description', $key) )

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