如何将元素添加到 SimpleXMLElement 对象内的数组中
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
print_r
输出中的“数组”并不是真正的数组 - 这只是 PHP 试图向您显示 SimpleXML 对象的状态,该对象有 4 个子元素,全部称为->addChild()
方法需要在要创建子节点的父节点上运行。$chat->messages->msg
将返回带有标签名称
的messages
的所有子节点的列表;正如您所发现的,如果 SimpleXML 需要对单个元素进行操作,它将假定您想要该列表中的第一项。这些都不是您想要的 - 您想要向节点
$chat->messages
添加一个新的
子节点,然后是该节点的两个子节点(
和
节点)。试试这个:
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 ofmessages
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:
SimpleXML 做了很多自动类型强制,这会让事情变得非常混乱。你尝试过这样的事情吗?
SimpleXML does a lot of automagic type coercion that can make things very confusing. Have you tried something like this?