0

I have this array and I want to get both ID and Description from it. And all I get is Array Array Array... :-)

function getCountries() {
$json = file_get_contents('http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/Utils/Countries');
$data = json_decode($json, TRUE);
$countries = array(); 
foreach($data['data']['item'] as $item) {
    $countries[] = $item;
}
//print_r($countries);
foreach($countries as $v)
{   
    echo $v.'<br />'; 
    }
}
getCountries();
1
  • 1
    what was the result on print_r($countries)? Commented May 23, 2011 at 19:42

3 Answers 3

2

Try this:

foreach($countries as $v)
{   
    // I am assuming $v is an array with `ID` and `Description` in it:
    echo $v['id']."  ".$v['description'].'<br />'; 
}

This is bacuse the json for item is:

"item":
   [{"id":"DE","description":"Deutschland"},
    {"id":"ES","description":"España"},
    {"id":"FR","description":"France"},
    {"id":"PT","description":"Portugal"},
    {"id":"UK","description":"United Kingdom"},
    {"id":"US","description":"United States"}]

where each item contains an id and description in an array

Sign up to request clarification or add additional context in comments.

Comments

2

You're getting that output because $v is an array, and you will have to reference the specific keys of $v to see the values, eg $v['id']. You can see the keys/values of $v are in your loop by doing print_r($v); instead of echo.

Comments

1
echo $v['id'] . '-' . $v['description'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.