How can PHP collect specified content from a JSON file?

You can decode JSON data into a PHP array or object using the json_decode() function, and then access specific content by indexing the array or accessing object properties.

For example, let’s say we have the following JSON data:

{
  "name": "John",
  "age": 30,
  "city": "New York"
}

You can use the following code to gather specific content:

$jsonData = '{
  "name": "John",
  "age": 30,
  "city": "New York"
}';

$data = json_decode($jsonData);

$name = $data->name;
$age = $data->age;
$city = $data->city;

echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
echo "City: " . $city . "<br>";

Output result:

Name: John
Age: 30
City: New York

Additionally, if the JSON data is an array containing multiple elements, you can use a foreach loop to iterate through and collect the content of each element.

bannerAds