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

发布于 2024-11-26 10:39:23 字数 2898 浏览 2 评论 0原文

我使用 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],而不是整个数组。可能是什么问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日 2024-12-03 10:39:23

print_r 输出中的“数组”并不是真正的数组 - 这只是 PHP 试图向您显示 SimpleXML 对象的状态,该对象有 4 个子元素,全部称为 < /code>

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

$chat->messages->msg 将返回带有标签名称 messages 的所有子节点的列表;正如您所发现的,如果 SimpleXML 需要对单个元素进行操作,它将假定您想要该列表中的第一项。

这些都不是您想要的 - 您想要向节点 $chat->messages 添加一个新的 子节点,然后是该节点的两个子节点( 节点)。

试试这个:

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

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?');
坏尐絯 2024-12-03 10:39:23

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

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

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.',
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文