-1

Sorry not very good with the whole JSON thing, from the json below, how would I output the title?

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

So far i've got:

<?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);
    ?>
mattmuirhead
  • 73
  • 1
  • 1
  • 11

2 Answers2

0

These are all just arrays, so:

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

You might want to iterate over $wiki['query']['search'], though.

ofrommel
  • 2,129
  • 14
  • 19
0

I don't know which title are you referring to since your JSON data (query) contains tons of titles.

Just use a foreach loop to get the bunch of the titles:

$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
}

The echo should show:

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