title: How to add an element to an array inside a SimpleXMLElement Object
body: <p>I have this object parsed using SimpleXML:</p>
  
  <pre><code>SimpleXMLElement Object
  (
  [contact] =&gt; SimpleXMLElement Object
      (
          [name] =&gt; Some guy
          [number] =&gt; **********
      )
  
  [messages] =&gt; SimpleXMLElement Object
      (
          [msg] =&gt; Array
              (
                  [0] =&gt; SimpleXMLElement Object
                      (
                          [from] =&gt; Some guy
                          [message] =&gt; Hey
                          [5] =&gt; SimpleXMLElement Object
                              (
                              )
  
                      )
  
                  [1] =&gt; SimpleXMLElement Object
                      (
                          [from] =&gt; Qasim Iqbal
                          [message] =&gt; Hows it going?
                      )
  
                  [2] =&gt; SimpleXMLElement Object
                      (
                          [from] =&gt; Some guy
                          [message] =&gt; Not bad... just doing some homework
                      )
  
                  [3] =&gt; SimpleXMLElement Object
                      (
                          [from] =&gt; Some guy
                          [message] =&gt; Im just kidding I'm playing games
                      )
  
                  [4] =&gt; SimpleXMLElement Object
                      (
                          [from] =&gt; Qasim Iqbal
                          [message] =&gt; lol...
                      )
  
              )
  
      )
  
  )
  </code></pre>
  
  <p>In my PHP file, the object is named $chat.
  My goal is to add another element to the [msg] array so the full array looks like this:</p>
  
  <pre><code>[msg] =&gt; Array
          (
              [0] =&gt; SimpleXMLElement Object
                  (
                      [from] =&gt; Some guy
                      [message] =&gt; Hey
                  )
  
              [1] =&gt; SimpleXMLElement Object
                  (
                      [from] =&gt; Qasim Iqbal
                      [message] =&gt; Hows it going?
                  )
  
              [2] =&gt; SimpleXMLElement Object
                  (
                      [from] =&gt; Some guy
                      [message] =&gt; Not bad... just doing some homework
                  )
  
              [3] =&gt; SimpleXMLElement Object
                  (
                      [from] =&gt; Some guy
                      [message] =&gt; Im just kidding I'm playing games
                  )
  
              [4] =&gt; SimpleXMLElement Object
                  (
                      [from] =&gt; Qasim Iqbal
                      [message] =&gt; lol...
                  )
  
              [5] =&gt; SimpleXMLElement Object
                  (
                      [from] =&gt; Some guy
                      [message] =&gt; what are you laughing at?
                  )
  
          )
  </code></pre>
  
  <p>Notice how the element with key "5" was added. I am trying to do it like this:</p>
  
  <pre><code>$chat-&gt;messages-&gt;msg-&gt;addChild(sizeof($chat-&gt;messages-&gt;msg));
  </code></pre>
  
  <p>But that for some reason doesnt work because $chat->messages->msg automatically is defined like $chat->messages->msg[0], and not the whole array. What could be the problem?</p>