Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Converting an XML string (<code>$buffer</code>) into a simplified array ignoring attributes and grouping child-elements with the same names:</p> <pre><code>function XML2Array(SimpleXMLElement $parent) { $array = array(); foreach ($parent as $name =&gt; $element) { ($node = &amp; $array[$name]) &amp;&amp; (1 === count($node) ? $node = array($node) : 1) &amp;&amp; $node = &amp; $node[]; $node = $element-&gt;count() ? XML2Array($element) : trim($element); } return $array; } $xml = simplexml_load_string($buffer); $array = XML2Array($xml); $array = array($xml-&gt;getName() =&gt; $array); </code></pre> <p>Result:</p> <pre><code>Array ( [aaaa] =&gt; Array ( [bbb] =&gt; Array ( [cccc] =&gt; Array ( [dddd] =&gt; [eeee] =&gt; ) ) ) ) </code></pre> <p>If you also want to have the attributes, they are available via JSON encoding/decoding of SimpleXMLElement. This is often the most easy quick'n'dirty solution:</p> <pre><code>$xml = simplexml_load_string($buffer); $array = json_decode(json_encode((array) $xml), true); $array = array($xml-&gt;getName() =&gt; $array); </code></pre> <p>Result:</p> <pre><code>Array ( [aaaa] =&gt; Array ( [@attributes] =&gt; Array ( [Version] =&gt; 1.0 ) [bbb] =&gt; Array ( [cccc] =&gt; Array ( [dddd] =&gt; Array ( [@attributes] =&gt; Array ( [Id] =&gt; id:pass ) ) [eeee] =&gt; Array ( [@attributes] =&gt; Array ( [name] =&gt; hearaman [age] =&gt; 24 ) ) ) ) ) ) </code></pre> <p>Take note that all these methods only work in the namespace of the XML document.</p>
    singulars
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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