dongpang2029 2015-04-30 19:59
浏览 61
已采纳

使用数组php添加或更新节点值

Sorry for possible newbie question, but I need to use xml for an import. I've figured it out so far but then scratched my eyeballs out while trying to figure out this (and searched for it)

I have this

<?php
$xml = new DOMDocument("1.0","UTF-8");
$container = $xml-> createElement("container");
$container = $xml-> appendChild($container);


for($n=0;$n<8;$n++){
    $maxnum[]=$n;
}
$xtags = $xml->CreateElement("tags",$maxnum); $xtags = $container->appendChild($xtags);
if($xml->save("stuff2.xml")){echo 'success';} else {echo 'not working';}
?>

Basically I dont want to create a child node for ever value of a loop. And I know thats not the proper way to do it, nor is array_values and so on. But it's what I want to achieve. I need to generate an XML file for wpall import so that I can succesfully bulk import to woocommerce. The specific attribute here i ize, and Im getting that usingfile_get_contents` from the server, parsing which xpath with a foreach loop. Problem is I want them all in a single field rather than having this

<size>
<value>1</value>
<value>2</value>
</size> 

and so on, when I want it like this <size><value>1,2,3....</value></size>

Any help, please?

  • 写回答

1条回答 默认 最新

  • dqc42632 2015-04-30 20:08
    关注

    Then you do not need to use an array, you can do something like:

    echo "<size><value>";
    foreach($n as $n){
    $string="$string, ".$n;
    }
    echo $string;
    echo" </value></size>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器