只解码Twitter的一个密钥';在PHP中创建JSON并将其放入数组中


Decoding only one key of Twitter's JSON in PHP and putting it in an array

使用Twitter的搜索API,我返回了大量与我无关的信息。我想用填充一个数组,其中只包含推文的内容,而不包含元数据。

下面是一个请求和响应的示例。因此,基本上,我想用text键的值填充一个数组。

我想答案应该在某种for循环中,但我不知道从哪里开始。

这样的东西可以做到:

<?
    $json = json_decode(file_get_contents('http://search.twitter.com/search.json?q=laughter&rpp=100&result_type=recent&lang=en'));
    $l = count($json->results);
    for ($i=0; $i < $l; $i++) { 
        echo $json->results[$i]->text . '<br/>';
    }
?>

注意:如果不允许file_get_contents远程读取,请使用所需的任何读取方法。

试试这个:

将JSON转换为数组PHP

<?php
    //get json
    $foo= file_get_contents('http://search.twitter.com/search.json?q=stackoverflow&rpp=10&result_type=recent&lang=en');
    //convert to array
    $var = json_decode( $foo , true );
    //print array
    print_r( $var );
?>

将数组PHP转换为JSON

<?php
    //declarate array
    $foo = array( 1 ,2 ,3 ,4 ,5 , 6 => 7, 8);
    //convert to json
    $var = json_encode( $foo );
    //print json
    print_r( $var );
?>

阅读:

从PHP 5.2.0开始,JSON扩展被绑定并编译成PHP默认情况下。

查看并阅读此JSON PHP文档

如果您使用PHP<5.2、使用JSON类对象

再见!