Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You'll want to use the <a href="http://ca.php.net/manual/en/simplexmlelement.children.php" rel="nofollow"><code>SimpleXMLElement::children()</code></a> function to find the children of a property. </p> <p><strong>Example:</strong></p> <pre><code>&lt;?php $string = &lt;&lt;&lt;END &lt;?xml version="1.0"?&gt; &lt;properties&gt; &lt;property&gt; &lt;ID&gt;243&lt;/ID&gt; &lt;area&gt;5,000&lt;/area&gt; &lt;bathrooms&gt;5&lt;/bathrooms&gt; &lt;bedrooms&gt;4&lt;/bedrooms&gt; &lt;images&gt; &lt;image&gt;http://urltoimage.com/image1.jpg&lt;/image&gt; &lt;image&gt;http://urltoimage.com/image2.jpg&lt;/image&gt; &lt;/images&gt; &lt;/property&gt; &lt;property&gt; &lt;ID&gt;332&lt;/ID&gt; &lt;garage&gt;2&lt;/garage&gt; &lt;bathrooms&gt;2&lt;/bathrooms&gt; &lt;images&gt; &lt;image&gt;http://urltoimage.com/image5.jpg&lt;/image&gt; &lt;image&gt;http://urltoimage.com/image1.jpg&lt;/image&gt; &lt;/images&gt; &lt;/property&gt; &lt;/properties&gt; END; // Load the XML using the SimpleXML class. $xml = simplexml_load_string($string); // Loop through all of the properties. foreach ( $xml-&gt;property as $property ) { // Reset the property tags array for this property. $property_tags = array(); foreach ( $property-&gt;children() as $children ) { // If a tag was found, add it to the array. if ( ! empty($children[0]) ) $property_tags[] = $children[0]-&gt;getName(); } // Output the list to the screen (this could be removed). print_r($property_tags); } </code></pre> <p><strong>Output:</strong></p> <pre><code>Array ( [0] =&gt; ID [1] =&gt; area [2] =&gt; bathrooms [3] =&gt; bedrooms [4] =&gt; images ) Array ( [0] =&gt; ID [1] =&gt; garage [2] =&gt; bathrooms [3] =&gt; images ) </code></pre> <p>If you'd rather get a list of all available tags (for all property contained in the XML document), simply do this:</p> <pre><code>// Loop through all of the properties. foreach ( $xml-&gt;property as $property ) { foreach ( $property-&gt;children() as $children ) { // If a tag was found, add it to the array if it's not already in it. if ( ! empty($children[0]) &amp;&amp; ! in_array($children[0]-&gt;getName(), $property_tags) ) $property_tags[] = $children[0]-&gt;getName(); } } // Output the list to the screen (this could be removed). print_r($property_tags); </code></pre> <p><strong>Output:</strong></p> <pre><code>Array ( [0] =&gt; ID [1] =&gt; area [2] =&gt; bathrooms [3] =&gt; bedrooms [4] =&gt; images [5] =&gt; garage ) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload