Search code examples
phparraysobjectsimplexmladdchild

How to add an element to an array inside a SimpleXMLElement Object


I have this object parsed using SimpleXML:

SimpleXMLElement Object
(
[contact] => SimpleXMLElement Object
    (
        [name] => Some guy
        [number] => **********
    )

[messages] => SimpleXMLElement Object
    (
        [msg] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [from] => Some guy
                        [message] => Hey
                        [5] => SimpleXMLElement Object
                            (
                            )

                    )

                [1] => SimpleXMLElement Object
                    (
                        [from] => Qasim Iqbal
                        [message] => Hows it going?
                    )

                [2] => SimpleXMLElement Object
                    (
                        [from] => Some guy
                        [message] => Not bad... just doing some homework
                    )

                [3] => SimpleXMLElement Object
                    (
                        [from] => Some guy
                        [message] => Im just kidding I'm playing games
                    )

                [4] => SimpleXMLElement Object
                    (
                        [from] => Qasim Iqbal
                        [message] => lol...
                    )

            )

    )

)

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:

[msg] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [from] => Some guy
                    [message] => Hey
                )

            [1] => SimpleXMLElement Object
                (
                    [from] => Qasim Iqbal
                    [message] => Hows it going?
                )

            [2] => SimpleXMLElement Object
                (
                    [from] => Some guy
                    [message] => Not bad... just doing some homework
                )

            [3] => SimpleXMLElement Object
                (
                    [from] => Some guy
                    [message] => Im just kidding I'm playing games
                )

            [4] => SimpleXMLElement Object
                (
                    [from] => Qasim Iqbal
                    [message] => lol...
                )

            [5] => SimpleXMLElement Object
                (
                    [from] => Some guy
                    [message] => what are you laughing at?
                )

        )

Notice how the element with key "5" was added. I am trying to do it like this:

$chat->messages->msg->addChild(sizeof($chat->messages->msg));

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?


Solution

  • SimpleXML does a lot of automagic type coercion that can make things very confusing. Have you tried something like this?

    $messages = (array) $chat->messages;
    $messages[] = (object) array(
      'from' => 'Barak Obama',
      'message' => 'I love you.',
    );