title: PHP foreach in foreach problem
body: <p>I want to insert some value from first and second foreach into database, but I met some trouble. I write my problem in the code. I can not solve the two loop problem. I ask for a help.</p>
  
  <pre><code>&lt;?php
  header('Content-type:text/html; charset=utf-8');
  set_time_limit(0);
  require_once ('../conn.php'); 
  require_once ('../simple_html_dom.php');
  $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&amp;rsz=large&amp;q=obama&amp;key={api-key}";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_REFERER, $url);
  $body = curl_exec($ch);
  curl_close($ch);
  $data = json_decode($body);
  foreach ($data-&gt;responseData-&gt;results as $result) { 
  $title = html_entity_decode($result-&gt;titleNoFormatting);
  $link = html_entity_decode($result-&gt;unescapedUrl);
  $html = @file_get_html($link );
  foreach(@$html-&gt;find('h3') as $element) {
       $table=$element;
           echo $table;// here while the $table is empty, echo is null.  
  }
  echo $table;// here while the $table is empty, echo will repeat the prev $table value.  
  mysql_query("SET NAMES utf8");
   mysql_query("INSERT INTO ...");// I want insert all the $title and $table into database.
  } 
  echo '&lt;hr /&gt;';
  }
  ?&gt;
  </code></pre>
  
  <p>I print the result <code>while the $table is empty, echo will repeat the prev $table value</code>.</p>
  
  <pre><code>Organizing for America | BarackObama.com
  
  Barack Obama - Wikipedia, the free encyclopedia
  
  President Barack Obama | The White House
  President Obama Nominates William Francis Kuntz, II to the United States District Court//the prev value
  
  Change.gov - The Official Web Site of the
  President Obama Nominates William Francis Kuntz, II to the United States District Court//here the $table is empty, it will repeat the prev $table value, and it should be empty.
  
  Barack Obama on Myspace
  Idle Friends▼
  
  ob (obama) on Twitter
  Piè di pagina
  
  Barack Obama
  Advertise with the NY Daily News!
  
  Barack Obama on the Issues
  Voting Record
  </code></pre>