title: Using PHP foreach loop to parse Simple XML result from AWS Cloudsearch API response
body: <p>This is my first time asking a question here, and I am also a novice at PHP and simple XML, but am determined to learn this.  I've searched through all of the relevant questions that have been asked before but am having trouble applying it to my situation although it looks so similar.  Really frustrating :(.  </p>
  
  <p>I am trying to read an API response from Amazon Cloudsearch in XML format. </p>
  
  <p>Here is the original XML response:</p>
  
  <pre><code>&lt;results xmlns="http://cloudsearch.amazonaws.com/2011-02-01/results"&gt;
  &lt;rank&gt;-text_relevance&lt;/rank&gt;
  &lt;match-expr&gt;(label '"mango"')&lt;/match-expr&gt;
  &lt;hits found="99" start="0"&gt;
  &lt;hit id="mango1to100_csv_31"&gt;
  &lt;d name="imageurl"&gt;
  http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/66650066_51.jpg
  &lt;/d&gt;
  &lt;/hit&gt;
  &lt;hit id="mango1to100_csv_10"&gt;
  &lt;d name="imageurl"&gt;
  http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650060_34.jpg
  &lt;/d&gt;
  &lt;/hit&gt;
  &lt;hit id="mango1to100_csv_11"&gt;
  &lt;d name="imageurl"&gt;
  http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650062_MQ.jpg
  &lt;/d&gt;
  &lt;/hit&gt;
  &lt;hit id="mango1to100_csv_12"&gt;
  &lt;d name="imageurl"&gt;
  http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650067_58.jpg
  &lt;/d&gt;
  &lt;/hit&gt;
  &lt;hit id="mango1to100_csv_13"&gt;
  &lt;d name="imageurl"&gt;
  http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650034_02.jpg
  &lt;/d&gt;
  &lt;/hit&gt;
  &lt;hit id="mango1to100_csv_14"&gt;
  &lt;d name="imageurl"&gt;
  http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650036_MQ.jpg
  &lt;/d&gt;
  &lt;/hit&gt;
  &lt;hit id="mango1to100_csv_15"&gt;
  &lt;d name="imageurl"&gt;
  http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650038_34.jpg
  &lt;/d&gt;
  &lt;/hit&gt;
  &lt;hit id="mango1to100_csv_16"&gt;
  &lt;d name="imageurl"&gt;
  http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650039_39.jpg
  &lt;/d&gt;
  &lt;/hit&gt;
  &lt;hit id="mango1to100_csv_17"&gt;
  &lt;d name="imageurl"&gt;
  http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650070_33.jpg
  &lt;/d&gt;
  &lt;/hit&gt;
  &lt;hit id="mango1to100_csv_18"&gt;
  &lt;d name="imageurl"&gt;
  http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650040_02.jpg
  &lt;/d&gt;
  &lt;/hit&gt;
  &lt;/hits&gt;
  &lt;facets/&gt;
  &lt;info rid="12345" time-ms="3" cpu-time-ms="0"/&gt;
  &lt;/results&gt; 
  </code></pre>
  
  <p>I've used SimpleXML to start:</p>
  
  <pre><code>SimpleXMLElement Object
  (
  [rank] =&gt; -text_relevance
  [match-expr] =&gt; (label '"mango"')
  [hits] =&gt; SimpleXMLElement Object
      (
          [@attributes] =&gt; Array
              (
                  [found] =&gt; 99
                  [start] =&gt; 0
              )
  
          [hit] =&gt; Array
              (
                  [0] =&gt; SimpleXMLElement Object
                      (
                          [@attributes] =&gt; Array
                              (
                                  [id] =&gt; mango1to100_csv_31
                              )
  
                          [d] =&gt; http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/66650066_51.jpg
                      )
  
                  [1] =&gt; SimpleXMLElement Object
                      (
                          [@attributes] =&gt; Array
                              (
                                  [id] =&gt; mango1to100_csv_10
                              )
  
                          [d] =&gt; http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650060_34.jpg
                      )
  
                  [2] =&gt; SimpleXMLElement Object
                      (
                          [@attributes] =&gt; Array
                              (
                                  [id] =&gt; mango1to100_csv_11
                              )
  
                          [d] =&gt; http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650062_MQ.jpg
                      )
  
                  [3] =&gt; SimpleXMLElement Object
                      (
                          [@attributes] =&gt; Array
                              (
                                  [id] =&gt; mango1to100_csv_12
                              )
  
                          [d] =&gt; http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650067_58.jpg
                      )
  
                  [4] =&gt; SimpleXMLElement Object
                      (
                          [@attributes] =&gt; Array
                              (
                                  [id] =&gt; mango1to100_csv_13
                              )
  
                          [d] =&gt; http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650034_02.jpg
                      )
  
                  [5] =&gt; SimpleXMLElement Object
                      (
                          [@attributes] =&gt; Array
                              (
                                  [id] =&gt; mango1to100_csv_14
                              )
  
                          [d] =&gt; http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650036_MQ.jpg
                      )
  
                  [6] =&gt; SimpleXMLElement Object
                      (
                          [@attributes] =&gt; Array
                              (
                                  [id] =&gt; mango1to100_csv_15
                              )
  
                          [d] =&gt; http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650038_34.jpg
                      )
  
                  [7] =&gt; SimpleXMLElement Object
                      (
                          [@attributes] =&gt; Array
                              (
                                  [id] =&gt; mango1to100_csv_16
                              )
  
                          [d] =&gt; http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650039_39.jpg
                      )
  
                  [8] =&gt; SimpleXMLElement Object
                      (
                          [@attributes] =&gt; Array
                              (
                                  [id] =&gt; mango1to100_csv_17
                              )
  
                          [d] =&gt; http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650070_33.jpg
                      )
  
                  [9] =&gt; SimpleXMLElement Object
                      (
                          [@attributes] =&gt; Array
                              (
                                  [id] =&gt; mango1to100_csv_18
                              )
  
                          [d] =&gt; http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650040_02.jpg
                      )
  
              )
  
      )
  
  [facets] =&gt; SimpleXMLElement Object
      (
      )
  
  [info] =&gt; SimpleXMLElement Object
      (
          [@attributes] =&gt; Array
              (
                  [rid] =&gt; 12345
                  [time-ms] =&gt; 3
                  [cpu-time-ms] =&gt; 0
              )
  
      )
  
  )
  </code></pre>
  
  <p>I am trying to read and display the images for each item shown based on the URL provided in the result.  I know I should be using a "foreach" loop here but I cant seem to get it right, so here is the long way I've done it:</p>
  
  <pre><code>&lt;?php
  $feed_url = 'http://www.myapicallexample.com';
  $feed = simplexml_load_file($feed_url);
  
  echo "&lt;ul&gt;";
  echo "&lt;li&gt;&lt;img src='", $feed-&gt;hits[0]-&gt;hit[1]-&gt;d,"'&gt;&lt;/li&gt;";
  echo "&lt;li&gt;&lt;img src='", $feed-&gt;hits[0]-&gt;hit[2]-&gt;d,"'&gt;&lt;/li&gt;";
  echo "&lt;li&gt;&lt;img src='", $feed-&gt;hits[0]-&gt;hit[3]-&gt;d,"'&gt;&lt;/li&gt;";
  echo "&lt;li&gt;&lt;img src='", $feed-&gt;hits[0]-&gt;hit[4]-&gt;d,"'&gt;&lt;/li&gt;";
  echo "&lt;li&gt;&lt;img src='", $feed-&gt;hits[0]-&gt;hit[5]-&gt;d,"'&gt;&lt;/li&gt;";
  echo "&lt;li&gt;&lt;img src='", $feed-&gt;hits[0]-&gt;hit[6]-&gt;d,"'&gt;&lt;/li&gt;";
  echo "&lt;li&gt;&lt;img src='", $feed-&gt;hits[0]-&gt;hit[7]-&gt;d,"'&gt;&lt;/li&gt;";
  echo "&lt;/ul&gt;";
  ?&gt;
  </code></pre>
  
  <p>I've tried various combinations of the foreach statement from looking at answers to other questions but nothing works.  I'm ready to throw my computer out the window.  Thanks in advance!  </p>
  
  <p>Addendum:  Here is an example of one of my many prior boneheaded attempts :)</p>
  
  <pre><code>&lt;?php
  $feed_url = 'http://www.myapicallexample.com';
  $xml = simplexml_load_file($feed_url);
  
  $imageurl = $xml-&gt;hits[0]-&gt;hit[1]-&gt;d;
  
  foreach($imageurl as $a)
  {
    echo $a;
  }
  
  
  
  ?&gt;
  </code></pre>