【问题标题】:Putting JSON results into a php array将 JSON 结果放入 php 数组
【发布时间】:2013-07-09 17:36:54
【问题描述】:

我有以下数组

Array
(
[responseData] => Array
      (
         [results] => Array
            (
                [0] => Array
                    (
                        [GsearchResultClass] => GwebSearch
                        [unescapedUrl] => http://en.wikipedia.org/wiki/JH_(hash_function)
                        [url] => http://en.wikipedia.org/wiki/JH_(hash_function)
                        [visibleUrl] => en.wikipedia.org
                        [cacheUrl] => http://www.google.com/search?q=cache:jxOefvJSQXUJ:en.wikipedia.org
                        [title] => JH (hash function) - Wikipedia, the free encyclopedia
                        [titleNoFormatting] => JH (hash function) - Wikipedia, the free encyclopedia
                        [content] => JH is a cryptographic hash function submitted to the NIST hash function   competition by Hongjun Wu. Though chosen as one of the five finalists of the   competition ...
                    )

                [1] => Array
                    (
                        [GsearchResultClass] => GwebSearch
                        [unescapedUrl] => http://www.jhaudio.com/
                        [url] => http://www.jhaudio.com/
                        [visibleUrl] => www.jhaudio.com
                        [cacheUrl] => http://www.google.com/search?q=cache:rO6NylpvTx8J:www.jhaudio.com
                        [title] => JH Audio: Custom In-Ear Monitors | In Ear Monitor
                        [titleNoFormatting] => JH Audio: Custom In-Ear Monitors | In Ear Monitor
                        [content] => Custom In-Ear Monitors by JHAudio - manufacturers of premium custom in ear   monitors. JH Audio's products are a direct result of 25 years of live audio mixing ...
                    )

                [2] => Array
                    (
                        [GsearchResultClass] => GwebSearch
                        [unescapedUrl] => http://www.jhaudio.com/collection/jha-pro-music-products
                        [url] => http://www.jhaudio.com/collection/jha-pro-music-products
                        [visibleUrl] => www.jhaudio.com
                        [cacheUrl] => http://www.google.com/search?q=cache:YY9q-E00yKkJ:www.jhaudio.com
                        [title] => JHA Pro Music Products | Custom In-Ear Monitors by JH Audio
                        [titleNoFormatting] => JHA Pro Music Products | Custom In-Ear Monitors by JH Audio
                        [content] => JHA Pro Music Products by JHAudio - manufacturers of premium custom in ear   monitors. JH Audio's products are a direct result of 25 years of live audio mixing ...
                    )

                [3] => Array
                    (
                        [GsearchResultClass] => GwebSearch
                        [unescapedUrl] => http://www.youtube.com/watch?v=JsQN3yZjRCI
                        [url] => http://www.youtube.com/watch%3Fv%3DJsQN3yZjRCI
                        [visibleUrl] => www.youtube.com
                        [cacheUrl] => http://www.google.com/search?q=cache:Dk4oETmQLNEJ:www.youtube.com
                        [title] => monta equina zagalo de j.h x gitana de la fortuna - YouTube
                        [titleNoFormatting] => monta equina zagalo de j.h x gitana de la fortuna - YouTube
                        [content] => Mar 5, 2010 ... caballos de exposicion en la modalidad de trote y galope, fecha de monta 1 de   febrero de 2010....
                    )

            )

我正在尝试使用 foreach 循环在 php 中解析它,以便显示 url、标题和内容。但是我似乎无法正确解析代码。下面的代码给了我一个“响应数据”无法识别的错误。

foreach($json as value)

echo $value [responsedata];

当我将它留在 echo $value 时,它​​给了我数字 200,这是 responsestatus 的值。当我尝试

foreach( $json =>url => title => content as $value)

它无法识别“=”号。

有什么想法吗??如果你还没有从帖子中得到 JSON 或 php,我对 JSON 或 php 并不太熟悉:)

TIA

【问题讨论】:

    标签: php json parsing search-engine


    【解决方案1】:

    FTR,你发布的是原生数组,不是“json数组”,试试这个:

    $rawArray = get_results_somehow();
    
    // Flatten the array to make it easier to work with
    $results = $rawArray['responseData']['results'];
    
    foreach ($results as $result) {
    
        // Should now see the expected values
        var_dump($result);
    }
    

    【讨论】:

      【解决方案2】:

      该数据已经是数组形式。您需要像这样迭代 $array['responseData']['results']。

      $arr = $array['responseData']['results'];
      
      foreach($arr as $k){
         echo $k['url'];
         echo $k['title'];
         echo $k['content'];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-11
        • 1970-01-01
        • 2016-06-06
        相关资源
        最近更新 更多