创建动态XML元素php

I need to create the Dynamic XML Elements and i have almost done it for the Value tag but I am still having some issue with Name Tag in XML.

<?php    
    foreach ($shot as $shotx){
        $metadata=$xml->createElement("metadata");
        $clip->appendChild($metadata);
        $name=$xml->createElement("name","shotL");
        $metadata->appendChild($name);
        $value=$xml->createElement("value",$shotx);
        $metadata->appendChild($value); 
    ?>

The output of the Above code is almost as i have expected instead of Name Tag. so the current output is written below:

<metadata> <name>shotL</name> <value>value1</value></metadata> 

<metadata> <name>shotL</name> <value>value2</value> </metadata>

<metadata> <name>shotL</name> <value>value3</value> </metadata> 

<metadata> <name>shotL</name> <value>value4</value> </metadata>

<metadata> <name>shotL</name> <value>value5</value> </metadata>

I would also need to have the dynamic value in the name tag like Below:

<metadata> <name>**shotL**</name> <value>value1</value></metadata> 

<metadata> <name>**shotL.1**</name> <value>value2</value> </metadata>

<metadata> <name>**shotL.2**</name> <value>value3</value> </metadata> 

<metadata> <name>**shotL.3<**/name> <value>value4</value> </metadata>

<metadata> <name>**shotL.4**</name> <value>value5</value> </metadata>

Basically the name Tag must also contain incremental Value like:

shotL
shotL.1
shotL.2
shotL.3
shotL.4

Please help me , Thanking you in advance.

doukan4039
doukan4039 使用次数($shot)
接近 4 年之前 回复
doudang8824
doudang8824 如何用plain处理循环结束条件?$shot变量具有动态值a
接近 4 年之前 回复
dsaeyrq451928
dsaeyrq451928 使用plain而不是foreach,以便您可以跟踪元素索引
接近 4 年之前 回复

1个回答

so I used this Code to solve my Problem and thanks to@har07 Idea's.

<?php    
$endshotc = count($shot);
for ($i=0; $i < $endshotc; $i++){
       metadata=$xml->createElement("metadata");
        $clip->appendChild($metadata);
        $name=$xml->createElement("name","shotL".$i);
        $metadata->appendChild($name);
        $value=$xml->createElement("value",$shotx[$i]);
        $metadata->appendChild($value); 
}
    ?>

Thank you for all help.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问