如何从JSON输出

时间:2014-07-03 21:28:35

标签: php json

对不起整个JSON的事情不太好,从下面的json,我将如何输出标题?

http://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=world%20cup

到目前为止我已经:

<?php
      $url ="http://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=world%20cup";
      $json = file_get_contents($url);
      $wiki = json_decode($json, TRUE);

      var_dump($wiki);
    ?>

2 个答案:

答案 0 :(得分:0)

这些都只是数组,所以:

$wiki['query']['search'][0]['title'];

您可能希望迭代$wiki['query']['search']

答案 1 :(得分:0)

我不知道您指的是哪个标题,因为您的JSON数据(查询)包含大量标题。

只需使用foreach循环即可获得大量标题:

$url ="http://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=world%20cup";
$json = file_get_contents($url);
$wiki = json_decode($json, TRUE);
$titles = array();
foreach($wiki['query']['search'] as $values) {
    echo $values['title'] . '<br/>'; // echo it
    $titles[] = $values['title']; // or store inside another array (container) if you want to process it to something else
}

回声应显示:

World Cup
List of world sports championships
FIFA World Cup
2010 FIFA World Cup
2006 FIFA World Cup
FIS Alpine Ski World Cup
2014 FIFA World Cup
1998 FIFA World Cup
1994 FIFA World Cup
2002 FIFA World Cup