title: How to convert XML into the array with PHP?
body: <p>I'm trying to split an XML in the array, below is my php codes:
  
     </p>
  
  <pre><code>  &lt;?php
       $xml = simplexml_load_file("test.xml") or die("Error: Cannot create object");
  
       foreach($xml-&gt;children() as $books) { 
          echo $books-&gt;title . "&lt;br&gt; "; 
          echo $books-&gt;tutor . "&lt;br&gt; "; 
          echo $books-&gt;duration . "&lt;br&gt; ";
          echo $books-&gt;price . "&lt;hr&gt;"; 
  
  
       }
    ?&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>Below is my XML codes:</p>
  
  <pre><code>&lt;?xml version = "1.0" encoding = "utf-8"?&gt;
  &lt;tutorialspoint&gt;
  
  &lt;course category = "JAVA"&gt;
    &lt;title lang = "en"&gt;Java&lt;/title&gt;
    &lt;tutor&gt;Gopal&lt;/tutor&gt;
    &lt;duration&gt;3&lt;/duration&gt;
    &lt;price&gt;$30&lt;/price&gt;
  &lt;/course&gt;
  
  &lt;course category = "HADOOP"&gt;
    &lt;title lang = "en"&gt;Hadoop&lt;/title&gt;.
    &lt;tutor&gt;Satish&lt;/tutor&gt;
    &lt;duration&gt;3&lt;/duration&gt;
    &lt;price&gt;$50&lt;/price&gt;
  &lt;/course&gt;
  
  &lt;course category = "HTML"&gt;
    &lt;title lang = "en"&gt;html&lt;/title&gt;
    &lt;tutor&gt;raju&lt;/tutor&gt;
    &lt;duration&gt;5&lt;/duration&gt;
    &lt;price&gt;$50&lt;/price&gt;
  &lt;/course&gt;
  
  &lt;course category = "WEB"&gt;
    &lt;title lang = "en"&gt;Web Technologies&lt;/title&gt;
    &lt;tutor&gt;Javed&lt;/tutor&gt;
    &lt;duration&gt;10&lt;/duration&gt;
    &lt;price&gt;$60&lt;/price&gt;
  &lt;/course&gt;
  
  &lt;/tutorialspoint&gt;
  </code></pre>
  
  <p>But it gave the output show me in below:
  <a href="https://i.stack.imgur.com/DQj6n.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>I want to convert XML into the array with PHP, but can't work. Actually the output I want like in below example codes:</p>
  
  <pre><code>Array
  (
  [0] =&gt; Array
      (
          [title] =&gt; Java
          [tutor] =&gt; Gopal
          [duration] =&gt; 3
          [price] =&gt; $30
      )
  
  [1] =&gt; Array
      (
          [title] =&gt; Hadoop
          [tutor] =&gt; Satish
          [duration] =&gt; 3
          [price] =&gt; $50
      )
  
  [2] =&gt; Array
      (
          [title] =&gt; HTML
          [tutor] =&gt; raju
          [duration] =&gt; 5
          [price] =&gt; $50
      )
  
  [3] =&gt; Array
      (
          [title] =&gt; Web Technologies
          [tutor] =&gt; Javed
          [duration] =&gt; 10
          [price] =&gt; $60
      )
  </code></pre>
  
  <p>I don't know how to set them into the array like above the example output. Hope someone can help me. Thanks.</p>