在PHP中显示数组中的数据

时间:2014-08-07 13:04:41

标签: php arrays json

这是我目前正在使用的代码,但它无效:

<?php
$jsonData = file_get_contents('http://example.com/bin/serp.php?engine=google&phrase=stackoverflow');

$a = json_decode($jsonData,true);
 foreach($a[0] as $item):
      echo '<span>'.$item['title'].'</span>';
      echo '<span>'.$item['url'].'</span>';
      echo '<span>'.$item['description'].';</span>';
 endforeach;
?>

但是,以上所有代码都在我的页面上显示;SSS;AAA;hhh;

以下是使用file_get_contents检索的JSON数据的副本:

Array ( [0] => Array ( [0] => Array ( [idx] => 0 [title] => Stack Overflow [description] => A language-independent collaboratively edited question and answer site for programmers. [url] => http://stackoverflow.com/ ) [1] => Array ( [idx] => 1 [title] => Stack Overflow - Wikipedia, the free encyclopedia [description] => Stack Overflow website logo.png · Stack Overflow.png. Screenshot of Stack Overflow as of December 2011. Web address · stackoverflow.com. Commercial? Yes. [url] => http://en.wikipedia.org/wiki/Stack_Overflow ) [2] => Array ( [idx] => 2 [title] => Why I no longer contribute to StackOverflow — Michael T. Richter [description] => I was active in the StackOverflow (and the broader Stack Exchange) community for a while. I no longer am. Here's why. [url] => http://michael.richter.name/blogs/why-i-no-longer-contribute-to-stackoverflow ) [3] => Array ( [idx] => 3 [title] => Newest 'stackoverflow' Questions - Meta Stack Exchange [description] => Download data for StackOverflow User Survey? The DropBox link to download the data for this survey is expired. Is there another place to get the data? [url] => http://meta.stackexchange.com/questions/tagged/stackoverflow ) [4] => Array ( [idx] => 4 [title] => Browse Queries - Stack Exchange Data Explorer [description] => StackOverflow Rank and Percentile. nov 4 12 Cade Roux. 56. 29k views ... Most popular StackOverflow tags in May 2010. aug 27 11 lukasz.lew. 10. 29k views ... [url] => http://data.stackexchange.com/stackoverflow/queries ) [5] => Array ( [idx] => 5 [title] => Return to StackOverflow.org [description] => StackOverflow.org began as the merging of two ideas that have been kicking around in my head for years. First, I wanted a dorky programming-related domain  ... [url] => http://stackoverflow.org/ ) [6] => Array ( [idx] => 6 [title] => StackOverflow Update: 560M Pageviews a Month, 25 Servers, and ... [description] => The network of sites that make up StackExchange, which includes StackOverflow, is now ranked 54th for traffic in the world; they have 110 sites ... [url] => http://highscalability.com/blog/2014/7/21/stackoverflow-update-560m-pageviews-a-month-25-servers-and-i.html ) ) ) 

有人能告诉我我在这里做错了吗?

2 个答案:

答案 0 :(得分:0)

您对json_decode()的调用已经返回一个数组,因此无需包含在另一个数组中:

$a = array(json_decode($jsonData,true));

应该是:

$a = json_decode($jsonData,true);

另外,如果var_dump来自$jsonData而不是$a,则该数组的深度为两级,您需要更深一级才能达到您的值:

foreach($a[0] as $item):

答案 1 :(得分:0)

这将为您提供一个不是数组的对象

尝试echo '<span>'.$item->title.'</span>';

或从此行中删除数组

$a = array(json_decode($jsonData,true));