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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?