dsv17139 2015-08-20 07:09
浏览 33
已采纳

更改当前子项的值 - SimpleXML PHP

The setup is quite simple. How do you use the children() iterating function of SimpleXML and then modify the value of the current node? Here's a scenario below:

<Ad>
  <Unit>Value</Unit>
  <Size>Value</Size>
  <Time>Value</Time>
</Ad>

I'm trying to use children() and then to assign to the current Child to change "Value" in all of the above nodes.

foreach ($parent->children() as $child){
  $child= "New Value"; // Proper way to access $child's value without overwriting the entire object???
}

Thanks in advance!

EDIT: I understand you can simply do the following:

$parent->Unit = "New Value"; 

I'm asking specifically in the case of using the children() method, how can you access the value of the current child object?

  • 写回答

2条回答 默认 最新

  • douhui8454 2015-08-20 18:44
    关注

    When a SimpleXMLElement represents a single element (rather than a collection of elements with the same name), index [0] can be used as a self-reference.

    Writing to that self-reference uses the overloaded assignment that you can use in other contexts to set the text content of a node.

    So the code you are looking for is as simple as:

    foreach ($parent->children() as $child) {
        $child[0] = "New Value";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上