`

Handling data in a PHP JSON Object

    博客分类:
  • php
阅读更多
Trends data from Twitter Search API in JSON.

Grabbing the file using:
$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);

How do I work with data from this object. As an array? Only really need to extract data from the [name] values.

JSON object contains:
stdClass Object
(
    [trends] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Vote
                    [url] => http://search.twitter.com/search?q=Vote
                )

            [1] => stdClass Object
                (
                    [name] => Halloween
                    [url] => http://search.twitter.com/search?q=Halloween
                )

            [2] => stdClass Object
                (
                    [name] => Starbucks
                    [url] => http://search.twitter.com/search?q=Starbucks
                )

            [3] => stdClass Object
                (
                    [name] => #flylady
                    [url] => http://search.twitter.com/search?q=%23flylady
                )

            [4] => stdClass Object
                (
                    [name] => #votereport
                    [url] => http://search.twitter.com/search?q=%23votereport
                )

            [5] => stdClass Object
                (
                    [name] => Election Day
                    [url] => http://search.twitter.com/search?q=%22Election+Day%22
                )

            [6] => stdClass Object
                (
                    [name] => #PubCon
                    [url] => http://search.twitter.com/search?q=%23PubCon
                )

            [7] => stdClass Object
                (
                    [name] => #defrag08
                    [url] => http://search.twitter.com/search?q=%23defrag08
                )

            [8] => stdClass Object
                (
                    [name] => Melbourne Cup
                    [url] => http://search.twitter.com/search?q=%22Melbourne+Cup%22
                )

            [9] => stdClass Object
                (
                    [name] => Cheney
                    [url] => http://search.twitter.com/search?q=Cheney
                )

        )

    [as_of] => Mon, 03 Nov 2008 21:49:36 +0000
)


<?php

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

foreach ( $json_output->trends as $trend )
{
    echo "{$trend->name}\n";
}
分享到:
评论

相关推荐

    Ajax for Web Application Developers(Ajax网站开发)

    Creating a Utilities Object Handling Status Codes with an HTTP Object Part III: Creating Reusable Components Chapter 10. Accordion Getting Started Creating the Accordion Object Panel ...

    Learn.PHP.7.148421729

    This new book on PHP 7 introduces writing solid, secure, object-oriented code in the new PHP 7, carefully presented in a well-paced, clear fashion. In Learn PHP 7, programming examples take advantage ...

    Professional JavaScript for Web Developers, 3rd Edition

    the JSON data format as an alternative to XML Ajax techniques including the use of XMLHttpRequest object and CORS complex patterns including function currying, partial function application, and ...

    Professional JavaScript for Web Developers英文版

    fly graphicsJavaScript API changes in HTML5how browsers handle JavaScript errors and error handlingfeatures of JavaScript used to read and manipulate XML datathe JSON data format as an alternative to ...

    AJAX and PHP.pdf

    To go through the examples in this book you need PHP 5, a web server, and a database server. We have tested the code under several environments, but mostly with the Apache 2 web server, and MySQL ...

    JS前台框架.rar

    16 json2 This file creates a global JSON object containing two methods: stringify and parse. 17 jsPlumb jsPlumb provides a means for a developer to visually connect elements on their web pages.

    PHP5 完整官方 中文教程

    PHP at the Core: A Hacker's Guide to the Zend Engine The PHP 5 build system Extension structure Memory management Working with variables Writing functions Working with classes and objects Working with...

    Unity.in.Action.Multiplatform.Game.Development.in.Csharp

    Setting up player and inventory managers Programming the game managers 180 ■ in a collection object: List vs. Dictionary 178 Storing inventory 184 9 Connecting your game to the internet 195 9.1 ...

    Beginning.Csharp.6.0.Programming.with.Visual.Studio.2015.11190

    such as variables, flow control, and object-oriented programming, this authoritative text moves into more complicated topics, such as web and Windows programming and data access within both database ...

    OReilly Java Cookbook 3rd Edition

    From lambda expressions and JavaFX 8 to new support for network programming and mobile development, Java 8 brings a wealth of ...Processing JSON and XML for data storage Multithreading and concurrency

    Learning iOS Programming, 2nd Edition.pdf

    Chapter 8, Handling Data This chapter discusses how to handle data input, both from the application user and programmatically, and how to parse XML and JSON documents. The chapter also covers storing ...

    PHP手册2007整合中文版

    JSON 函数 LXXIV. KADM5 LXXV. LDAP Functions LXXVI. libxml Functions LXXVII. Lotus Notes Functions LXXVIII. LZF Functions LXXIX. Mail 邮件函数 LXXX. Mailparse Functions LXXXI. Math 数学函数 LXXXII. ...

    PHP5中文参考手册

    PHP at the Core: A Hacker's Guide to the Zend Engine The PHP 5 build system Extension structure Memory management Working with variables Writing functions Working with classes and objects Working with...

    Python for Bioinformatics 第二版,最新版

    8.5.1 Create a New Data Type Using a Built-in Data Type 154 8.6 MAKING OUR CODE PRIVATE 154 8.7 ADDITIONAL RESOURCES 155 8.8 SELF-EVALUATION 156 Chapter 9 Introduction to Biopython 157 9.1 WHAT IS ...

    PHP官方手册中文版

    JSON 函数 LXXIV. KADM5 LXXV. LDAP Functions LXXVI. libxml Functions LXXVII. Lotus Notes Functions LXXVIII. LZF Functions LXXIX. Mail 邮件函数 LXXX. Mailparse Functions LXXXI. Math 数学函数 ...

    c20-programmers-3rd.epub

    Start with C++ fundamentals and the Deitels’ classic treatment of object-oriented programming—classes, inheritance, polymorphism, operator overloading and exception handling. Then discover ...

    GWT in Action

    of the JavaScript Object Notation (JSON) message format. We’ll explain what JSON is and provide an example of using it to communicate with the Yahoo Search API. Chapter 14 looks at GWT’s powerful ...

    Packt.Swift.Developing.iOS.Applications.2016

    Create a server in Swift to deliver JSON data to an iOS app Take advantage of Cocoapods to use third-party libraries Build games with SpriteKit and SceneKit Develop an app running on the cloud to act ...

    Learning.JavaScript.Add.Sparkle.and.Life.to.Your.Web.Pages.3rd.Edition.14

    Learn the latest in JavaScript developments, including Ajax and JSON Get concrete examples of common JavaScript usage patterns Deal with complications of different browser implementations Use best ...

Global site tag (gtag.js) - Google Analytics