如何在SimpleXMLElement对象内的数组中添加元素

时间:2011-07-22 21:50:45

标签: php arrays object simplexml addchild

我使用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...
                    )

            )

    )

)

在我的PHP文件中,该对象名为$ chat。 我的目标是在[msg]数组中添加另一个元素,这样整个数组就像这样:

[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?
                )

        )

注意添加了键“5”的元素。我想这样做:

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

但是由于某些原因,因为$ chat-> messages-> msg自动被定义为$ chat-> messages-> msg [0],而不是整个数组。可能是什么问题?

2 个答案:

答案 0 :(得分:1)

print_r输出中的“数组”实际上不是一个数组 - 这只是PHP尝试向您展示SimpleXML对象的状态,该对象有4个子元素,全部称为<msg>

->addChild()方法需要在您想要创建子节点的父节点上运行。

$chat->messages->msg将返回标记为messages的{​​{1}}所有子节点的列表;正如您所发现的,如果SimpleXML需要对单个元素进行操作,它将假定您需要该列表中的第一个项目。

这些都不是您想要的 - 您希望将新的<msg>子项添加到节点<msg>,然后将其中的两个子项添加到$chat->messages<from>节点)。

试试这个:

<message>

答案 1 :(得分:0)

SimpleXML做了很多自动类型强制,可以让事情变得非常混乱。你尝试过这样的事吗?

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