如何用php显示json内容?

时间:2014-02-22 05:51:00

标签: php json

这是我使用的代码:

<?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('money'));

它会在我的页面上显示:

["money",["moneygram","money network","money mutual","moneypak","money saving mom","moneyball","money","money converter","money order","moneygram walmart"]]

需要让它显示为这样吗?

moneygram, money network, money mutual, moneypak, money saving mom, moneyball, money, money converter, money order, moneygram walmart

1 个答案:

答案 0 :(得分:0)

做这样的事情: 使用json_decode,然后只需implode

<?php
  $json_code = '["money",["moneygram","money network","money mutual","moneypak","money saving mom","moneyball","money","money converter","money order","moneygram walmart"]]';

  $data = json_decode($json_code);

  echo implode(',',$data[1]);
?>

这将输出为:

moneygram, money network, money mutual, moneypak, money saving mom, moneyball, money, money converter, money order, moneygram walmart