这是我目前的代码:
<?php
$jsonData = file_get_contents('http://example.com/bin/serp.php?engine=google&phrase=stackoverflow');
$phpArray = json_decode($jsonData);
print_r($phpArray);
?>
现在它在我的页面上显示:
Array ( [0] => stdClass Object ( [idx] => 0 [title] => Stack Overflow [description] => A language-independent collaboratively edited question and answer site for programmers. [url] => http://stackoverflow.com/ ) [1] => stdClass Object ( [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] => stdClass Object ( [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] => stdClass Object ( [idx] => 3 [title] => StackOverflow Rank and Percentile - Stack Exchange Data Explorer [description] => StackOverflow Rank and Percentile WITH Rankings AS ( SELECT Id, Ranking = ROW_NUMBER() OVER(ORDER BY Reputation DESC) FROM Users ) ,Counts ... [url] => http://data.stackexchange.com/stackoverflow/query/6772/stackoverflow-rank-and-percentile ) [4] => stdClass Object ( [idx] => 4 [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 ) [5] => stdClass Object ( [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] => stdClass Object ( [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 ) )
这就是我试图在PHP页面上显示的内容(这只是前几行,但我想实际使用所有json数据):
1. <a href="http://stackoverflow.com/">Stack Overflow</a>
A language-independent collaboratively edited question and answer site for programmers.
2. <a href="http://en.wikipedia.org/wiki/Stack_Overflow">Stack Overflow - Wikipedia, the free encyclopedia</a>
Stack Overflow website logo.png · Stack Overflow.png. Screenshot of Stack Overflow as of December 2011. Web address · stackoverflow.com. Commercial? Yes.
这部分我似乎并不了解如何获取JSON数据并将其格式化为我想要的。所有数据都在那里,所以如果有人能告诉我我需要在当前代码中更改以使其正常工作,那将非常感激。
答案 0 :(得分:0)
解码json字符串时,您将获得一个数组。 如果你指定TRUE作为第二个参数,则数组将是关联的。
现在,您只需将数组的内容放在您喜欢的位置,例如:
// here we've the decoded json data
$a = array(
"link1",
"desc1",
"link2",
"desc2"
);
// for each data, we make an ordered list with data you need
// in your case you'll have the link and the description on every point
echo " <ol>
<li>{$a[0]}</li>
<li>{$a[1]}</li>
</ol>
<ol>
<li>{$a[2]}</li>
<li>{$a[3]}</li>
</ol> ";
告诉我,如果我不清楚,我会编辑我的答案。