这是我目前的代码:
<?php
echo file_get_contents("http://example.com/bin/serp.php?engine=google&phrase=stackoverflow&format=ARRAY");
?>
它在我的页面上显示:
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
)
)
我需要更改哪些内容才能显示出来?
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.
对此的任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
print_r()函数是一个调试助手。它不是用作序列化格式:
它可能导致数据丢失,例如:
$data = array(true, 1, false, 0, '');
print_r($data);
Array
(
[0] => 1
[1] => 1
[2] =>
[3] => 0
[4] =>
)
您在网址中有这个:
format=ARRAY
只要查看您使用的任何地方$_GET['format']
。您可能有更多有用的格式可供选择。如果你不这样做,那么实施一种合理的格式就是微不足道的,例如: JSON
答案 1 :(得分:0)
您可以使用PHP的foreach控制语句来实现此目的,并且在这里:
假设 $array
是 file_get_contents 的结果,并假设它保留了要显示的结果。
<?php foreach($array as $arr){?>
<a href="<?php echo $arr['url'];?>"><?php echo $arr['title'];?></a>
<?php echo $arr['description']; ?>
<?php } ?>
希望这可以帮助你......