Home  >  Article  >  Backend Development  >  How do I Handle Data in a PHP JSON Object?

How do I Handle Data in a PHP JSON Object?

Susan Sarandon
Susan SarandonOriginal
2024-10-19 10:20:30398browse

How do I Handle Data in a PHP JSON Object?

Handling Data in a PHP JSON Object

Working with JSON data in PHP can be straightforward if you understand the structure of the object. Consider fetching trends from the Twitter Search API as an example:

<code class="php">$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);</code>

The resulting $json_output is a stdClass object with a property called trends that contains an array of stdClass objects, with each representing a trend.

To access the trend names specifically, you can use a foreach loop:

<code class="php">foreach ( $json_output->trends as $trend ) {
    echo "{$trend->name}\n";
}</code>

This code will output a list of trend names, one per line.

The above is the detailed content of How do I Handle Data in a PHP JSON Object?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn