Reputation: 190
Please have a look at: http://codepad.org/uNlMsvwj for the json.
$json = json_decode($raw_json);
//print_r($json);
$count = count($json->response->results);
$i = 0;
foreach($json->response->results as $item){
echo($item->entries[$i]->displayname);echo "<br>";
echo($item->entries[$i]->location->street);echo " "; echo($item->entries[$i]->location->streetnumber);echo "<br>";
echo($item->entries[$i]->location->zipcode);echo " ";
echo($item->entries[$i]->location->city);echo "<br>";echo "<br>";
echo($item->entries[$i]->phonenumbers[0]->area);echo "/"; echo($item->entries[$i]->phonenumbers[0]->number);echo "<br>";
$i++;
}
The problem is that only the first elemet is printed. If i change $i manually with 1 i get the second.
I've been looking for 3 hours now and can't find a solution. Please forgive me if it is a beginner mistake.
Thanks
Update: thank you all for your fast help
Upvotes: 0
Views: 159
Reputation: 3170
Try this one,it works I tested it:
foreach($json->response->results[0]->entries as $item){
echo($item->displayname);echo "<br>";
echo($item->location->street);echo " ";
echo($item->location->streetnumber);echo "<br>";
echo($item->location->zipcode);echo " ";
echo($item->location->city);echo "<br>";echo "<br>";
echo($item->phonenumbers[0]->area);echo "/"; echo($item->phonenumbers[0]->number);echo "<br>";
}
Upvotes: 0
Reputation: 1445
Did you mean to iterate over the results like so?
foreach($json->response->results as $item) {
for ($i = 0; $i < count($item->entries); $i++) {
echo($item->entries[$i]->displayname);echo "<br>";
echo($item->entries[$i]->location->street);echo " ";
echo($item->entries[$i]->location->streetnumber);echo "<br>";
echo($item->entries[$i]->location->zipcode);echo " ";
echo($item->entries[$i]->location->city);echo "<br>";echo "<br>";
echo($item->entries[$i]->phonenumbers[0]->area);echo "/";
echo($item->entries[$i]->phonenumbers[0]->number);echo "<br>";
}
}
Also, you can simplify your echo
statements:
foreach($json->response->results as $item) {
for ($i = 0; $i < count($item->entries); $i++) {
echo $item->entries[$i]->displayname.'<br>';
echo $item->entries[$i]->location->street.' ';
echo $item->entries[$i]->location->streetnumber.'<br>';
echo $item->entries[$i]->location->zipcode.' ';
echo $item->entries[$i]->location->city.'<br><br>';
echo $item->entries[$i]->phonenumbers[0]->area.'/';
echo $item->entries[$i]->phonenumbers[0]->number.'<br>';
}
}
Upvotes: 1
Reputation: 2668
Try either for-loop instead of foreach:
$json = json_decode($raw_json);
//print_r($json);
$count = count($json->response->results);
foreach($json->response->results->entries as $item){
echo($item->displayname);echo "<br>";
echo($item->location->street);echo " ";
echo($item->location->streetnumber);echo "<br>";
echo($item->location->zipcode);echo " ";
echo($item->location->city);echo "<br>";echo "<br>";
echo($item->phonenumbers[0]->area);echo "/"; echo($item->phonenumbers[0]->number);echo "<br>";
}
...or include a for-loop inside the foreach:
$json = json_decode($raw_json);
//print_r($json);
$count = count($json->response->results);
foreach($json->response->results as $item){
for ($i = 0; $i < count($item->entries); $i++) {
echo($item->entries[$i]->displayname);echo "<br>";
echo($item->entries[$i]->location->street);echo " ";
echo($item->entries[$i]->location->streetnumber);echo "<br>";
echo($item->entries[$i]->location->zipcode);echo " ";
echo($item->entries[$i]->location->city);echo "<br>";echo "<br>";
echo($item->entries[$i]->phonenumbers[0]->area);echo "/";
echo($item->entries[$i]->phonenumbers[0]->number);echo "<br>";
}
}
Upvotes: 0
Reputation: 2617
Change
foreach($json->response->results as $item){
to
foreach($json->response->results->entries as $item){
and these lines
echo($item->entries[$i]->displayname);echo "<br>";
to
echo $item->displayname . "<br>";
So your code will look like:
foreach($json->response->results->entries as $item){
echo "{$item->displayname}<br />";
echo "{$item->location->street} {$item->location->streetnumber}<br />";
echo "{$item->zipcode}<br />";
echo "{$item->location->city}<br /><br />";
echo "{$item->phonenumbers[0]->area}/{$item->phonenumbers[0]->number}<br />";
}
Upvotes: 0
Reputation: 3297
You should echo like this:
echo $item->entries[$i]->displayname . "<br>";
the .
concatenates strings. Also echo
doesn't use any ()
.
Upvotes: 0