只解码Twitter的一个键';在PHP中使用JSON并将其放入数组中

只解码Twitter的一个键';在PHP中使用JSON并将其放入数组中,php,twitter,json,Php,Twitter,Json,使用Twitter的搜索API,我收到了大量与我无关的信息。我想用tweets的内容填充一个数组,而不是元数据 请求和响应的示例。所以基本上,我想用text键的值填充一个数组 我想答案应该是某种形式的for循环,但我不知道从哪里开始。类似的方法可以做到: <? $json = json_decode(file_get_contents('http://search.twitter.com/search.json?q=laughter&rpp=100&result_t

使用Twitter的搜索API,我收到了大量与我无关的信息。我想用tweets的内容填充一个数组,而不是元数据

请求和响应的示例。所以基本上,我想用
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/>';
    }
?>

注意:如果不允许远程读取文件内容,请使用您需要的任何读取方法。

尝试以下方法:

将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 );
?>

阅读:

从PHP5.2.0开始,JSON扩展被捆绑并编译到PHP中 默认情况下

看看这个

如果使用PHP<5.2,请使用JSON

再见