开发者

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
                    [m开发者_如何学Pythonessage] => 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?


The "array" in that print_r output is not really an array - that's just PHP's attempt to show you the state of the SimpleXML object, which has 4 child elements all called <msg>

The ->addChild() method needs to be run on the parent node that you want to create a child of.

$chat->messages->msg will return you a list of all child nodes of messages with the tag name <msg>; as you've discovered, if SimpleXML needs to act on a single element, it will assume you want the first item in that list.

Neither of these is what you want - you want to add a new <msg> child to the node $chat->messages, then two children of that (the <from> and <message> nodes).

Try this:

$new_item = $chat->messages->addChild('msg');
$new_item->addChild('from', 'Some guy');
$new_item->addChild('message', 'what are you laughing at?');


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.',
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜