Benjamin79
Benjamin79

Reputation: 190

foreach in php iterates only through the first JSON

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

Answers (5)

Suvash sarker
Suvash sarker

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

tmh
tmh

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

Kleskowy
Kleskowy

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

calcinai
calcinai

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

Joren
Joren

Reputation: 3297

You should echo like this:

echo $item->entries[$i]->displayname . "<br>";

the . concatenates strings. Also echo doesn't use any ().

Upvotes: 0

Related Questions