显示和链接数组中的信息

时间:2013-02-20 08:53:38

标签: php

我正在使用以下脚本从Google建议中提取信息:

<?php
function getKeywordSuggestionsFromGoogle($keyword) {
    $keywords = array();
    $data = file_get_contents('http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q='.urlencode($keyword));
    if (($data = json_decode($data, true)) !== null) {
        $keywords = $data[1];
    }

    return $keywords;
}

var_dump(getKeywordSuggestionsFromGoogle('business'));
?>

这是在浏览器中访问PHP文件时显示的内容:

array(10) { [0]=> string(14) "business cards" [1]=> string(22) "business letter format" [2]=> string(16) "business insider" [3]=> string(15) "business casual" [4]=> string(13) "business plan" [5]=> string(22) "business plan template" [6]=> string(13) "business week" [7]=> string(14) "business ideas" [8]=> string(17) "business for sale" [9]=> string(8) "business" } 

我需要弄清楚如何做两件事:

  1. 在我的网页上正确显示信息,使其如下所示:

    名片,商业信函格式等

  2. 我需要链接到我的域的每个关键字,如下所示:

    business cardsbusiness letter format,依此类推

  3. 任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

试试这样.............

<?php
function getKeywordSuggestionsFromGoogle($keyword) {
    $keywords = array();
    $data = file_get_contents('http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q='.urlencode($keyword));

    if (($data = json_decode($data, true)) !== null) {
        $keywords = $data[1];
    }

    return $keywords;
}

$result = getKeywordSuggestionsFromGoogle('business');


foreach($result as $key => $r)

  {

     echo '<a href = "http://www.mysite.com/keywords/'. $r.'">'. $r .'</a>';

   }
 ?>

如果您想引用某些链接,可以将echo $r置于<a href="#"><?php echo $r ?></a>之间