title: iterating over SimpleXML Objext PHP
body: <p>Here is what my object looks like with print_r (this is an object returned by the PHP SDK for the Amazon Web Services Simple DB.</p>
  
  <pre><code>[GetAttributesResult] =&gt; CFSimpleXML Object
              (
                  [Attribute] =&gt; Array
                      (
                          [0] =&gt; CFSimpleXML Object
                              (
                                  [Name] =&gt; data_datein
                                  [Value] =&gt; 2011-04-23
                              )
  
                          [1] =&gt; CFSimpleXML Object
                              (
                                  [Name] =&gt; data_estatus
                                  [Value] =&gt; 0
                              )
  
                          [2] =&gt; CFSimpleXML Object
                              (
                                  [Name] =&gt; data_status
                                  [Value] =&gt; 1
                              )
  
                          [3] =&gt; CFSimpleXML Object
                              (
                                  [Name] =&gt; data_title
                                  [Value] =&gt; Company Info
                              )
  
                          [4] =&gt; CFSimpleXML Object
                              (
                                  [Name] =&gt; data_tags
                                  [Value] =&gt; firsttag
                              )
  
                          [5] =&gt; CFSimpleXML Object
                              (
                                  [Name] =&gt; data_tags
                                  [Value] =&gt; secondtag
                              )
  
                          [6] =&gt; CFSimpleXML Object
                              (
                                  [Name] =&gt; data_tags
                                  [Value] =&gt; thirdtag
                              )
  
                          [7] =&gt; CFSimpleXML Object
                              (
                                  [Name] =&gt; data_files
                                  [Value] =&gt; company_info.flv
                              )
  
                          [8] =&gt; CFSimpleXML Object
                              (
                                  [Name] =&gt; data_id
                                  [Value] =&gt; 8993
                              )
  
                      )
  
              )
  </code></pre>
  
  <p>I have a function that iterates over the GetAttributesResult Object and creates an associative array that makes it easy to reference my fields by their names. One of my Names is data_tags, which is repeated an unknown number of times. I would like to return data_tags as a simple indexed array of those values. Here's my function, which doesn't work.</p>
  
  <pre><code>function attrToArray($select) { 
  $results = array(); 
  $x = 0; 
  foreach($select-&gt;body-&gt;GetAttributesResult as $result) { 
      foreach ($result as $field) { 
          if (array_key_exists($field,$results[$x])) {
              $results[$x][ (string) $field-&gt;Name ][] = (string) $field-&gt;Value;
          } else {
              $results[$x][ (string) $field-&gt;Name ] = (string) $field-&gt;Value; 
          }
      } 
      $x++; 
  } 
  return $results; 
  }
  </code></pre>
  
  <p>I don't know if this is the most elegant solution, but I don't see why it wouldn't work. array_key_exists doesn't return true. By mistake I was able to test as <code>in_array($field-Name,$results[$x])</code> and that built the array of my repeated $field->Name values... but it also converted all of the other values into single item nested array... so it would seem that it returned true more than I thought it would have. Although the hyphen in there was by mistake I meant to use -> which doesn't return true... so I'm very confused by what is going on there. Here's the print_r to show what came back.</p>
  
  <pre><code>Array ( [0] =&gt; Array ( 
  [data_datein] =&gt; 2011-04-23 
  [data_estatus] =&gt; 0 
  [data_status] =&gt; Array ( [0] =&gt; 1 ) 
  [data_title] =&gt; Array ( [0] =&gt; Company Info ) 
  [data_tags] =&gt; Array ( 
      [0] =&gt; firsttag
      [1] =&gt; secondtag 
      [2] =&gt; thirdtag ) 
  [data_files] =&gt; Array ( [0] =&gt; company_info.flv ) 
  [data_id] =&gt; Array ( [0] =&gt; 8993 ) ) ) 
  </code></pre>
  
  <p>Any pointers, suggestions or instruction on how I might handle this better... and at very least if someone can figure out how I can get to the above array without the nested arrays on the other non-redundant fields. Very much appreciated!</p>
  
  <p>Here is the <code>print_r()</code> of <code>$result</code>
      CFSimpleXML Object
      (
      [Attribute] => Array
          (
              [0] => CFSimpleXML Object
                  (
                      [Name] => data_datein
                      [Value] => 2011-04-23
                  )</p>
  
  <pre><code>        [1] =&gt; CFSimpleXML Object
              (
                  [Name] =&gt; data_estatus
                  [Value] =&gt; 0
              )
  
          [2] =&gt; CFSimpleXML Object
              (
                  [Name] =&gt; data_title
                  [Value] =&gt; 0001 01 Company Name
              )
  
          [3] =&gt; CFSimpleXML Object
              (
                  [Name] =&gt; data_status
                  [Value] =&gt; 1
              )
  
          [4] =&gt; CFSimpleXML Object
              (
                  [Name] =&gt; data_tags
                  [Value] =&gt; good stuff
              )
  
          [5] =&gt; CFSimpleXML Object
              (
                  [Name] =&gt; data_tags
                  [Value] =&gt; save tags
              )
  
          [6] =&gt; CFSimpleXML Object
              (
                  [Name] =&gt; data_tags
                  [Value] =&gt; tagger works
              )
  
          [7] =&gt; CFSimpleXML Object
              (
                  [Name] =&gt; data_files
                  [Value] =&gt; 0001_01_company_name.flv
              )
  
          [8] =&gt; CFSimpleXML Object
              (
                  [Name] =&gt; data_id
                  [Value] =&gt; yFKwIxjIhH
              )
  
      )
  
  )
  </code></pre>
  
  <p>and here is a <code>print_r()</code> of <code>$field</code> (iterated and separated by <code>&lt;hr&gt;</code> tags.)</p>
  
  <pre><code>  CFSimpleXML Object
    (
        [Name] =&gt; data_datein
        [Value] =&gt; 2011-04-23
    )
    &lt;hr&gt;CFSimpleXML Object
    (
        [Name] =&gt; data_estatus
        [Value] =&gt; 0
    )
    &lt;hr&gt;CFSimpleXML Object
    (
        [Name] =&gt; data_title
        [Value] =&gt; 0001 01 Company Name
    )
    &lt;hr&gt;CFSimpleXML Object
    (
        [Name] =&gt; data_status
        [Value] =&gt; 1
    )
    &lt;hr&gt;CFSimpleXML Object
    (
        [Name] =&gt; data_tags
        [Value] =&gt; good stuff
    )
    &lt;hr&gt;CFSimpleXML Object
    (
        [Name] =&gt; data_tags
        [Value] =&gt; save tags
    )
    &lt;hr&gt;CFSimpleXML Object
    (
        [Name] =&gt; data_tags
        [Value] =&gt; tagger works
    )
    &lt;hr&gt;CFSimpleXML Object
    (
        [Name] =&gt; data_files
        [Value] =&gt; 0001_01_company_name.flv
    )
    &lt;hr&gt;CFSimpleXML Object
    (
        [Name] =&gt; data_id
        [Value] =&gt; yFKwIxjIhH
    )
  </code></pre>