I've querying an API and I've got some troubles to display the code returned (JSON). If I do a var_dump, I have something like this:

var_dump($street['location']);

array(3) {
    ["latitude"]=> string(10) "52.6397278"
    ["street"]=> array(2) {
        ["id"]=> int(469)
        ["name"]=> string(23) "On or near Abbey Street"
    }
    ["longitude"]=> string(10) "-1.1322920"
} 

Now, usually, I will do something like that to have it displayed:

var_dump($street['location']);
echo '->latitude:' . $location['latitude'] . '<br/>';
foreach($location['street'] as $st){
    echo '-->street id:' . $st['id'] . '<br/>';
    echo '-->street name:' . $st['name'] . '<br/>';
}
echo '->Longitude:' . $location['longitude'] . '<br/>';

But I get:

array(3) {
    ["latitude"]=> string(10) "52.6397278"
    ["street"]=> array(2) {
        ["id"]=> int(469)
        ["name"]=> string(23) "On or near Abbey Street"
    }
    ["longitude"]=> string(10) "-1.1322920"
} ->latitude:5

Warning: Invalid argument supplied for foreach() in /home/pasd529/public_html/npia.php on line 102
->Longitude:5

The latitude / longitude are truncated and I can't get the streed id / name ...

Thanks for your help

share|improve this question

44% accept rate
feedback

3 Answers

up vote 1 down vote accepted

This should solve the problem with the street:

$street['location'] = // array - it's not really clear in your code.   
echo '->latitude:' . $street['location']['latitude'] . '<br/>';
echo '-->street id:' . $street['location']['street']['id'] . '<br/>';
echo '-->street name:' . $street['location']['street']['name'] . '<br/>';
echo '->Longitude:' . $street['location']['longitude'] . '<br/>';

You do not need to iterate ofer the array to access the street information.


If you want to use foreach for the array stored in $street['location']['street']:

// ...
foreach($street['location']['street'] as $key => $value){
   echo '-->street ', $key, ': ', $value, '<br />';
}
// ...

(Note that you can don't need to concatenate using . when you just want to echo something and can just use the ,.)

share|improve this answer
Thanks. Works fine. how do I know when I need to do a foreach or not ? – Olivier Oct 25 '11 at 21:39
@middus: this is debug code :( – Gianps Oct 25 '11 at 21:43
@Gianps Sorry, I don't understand what you're trying to tell me. – middus Oct 25 '11 at 21:51
@Olivier I've added an example to show you how foreach would work. – middus Oct 25 '11 at 21:53
Excellent, thank you for the help, much appreciate. @Gianps: yes, this might be code debugging, but sometimes people who are not developing all the day needs it ... And I REALLY APPRECIATE all the help I'm getting from here. – Olivier Oct 26 '11 at 8:23
show 2 more comments
feedback

You're dumping out one value ($street['location']), but are foreach looping on $location['street'] instead. So most likely you've got your foreach value reversed.

If the var_dump is to be trusted, then you don't even need the foreach loop:

echo '->latitude:' . $location['latitude'] . '<br/>';
echo '-->street id:' . $location['street']['id'] . '<br/>';
echo '-->street name:' . $location['street']['name'] . '<br/>';
share|improve this answer
feedback

$location['street'].. where did you assign it? is not $street['street']?

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.