0

私はphpとsimplexmlを使用しています。私の問題は「forで属性を追加すること」です。私のコードは次のとおりです。

for($i=1;$i<=$compteur;$i++)
    {
        $jeu->situation->question->choix->addChild('rep',"\n".$rep[$i]."\n");
        $jeu->situation->question->choix->rep->addAttribute('val',$i);

    }

結果:

                             <choix>
                <rep val="1">

                </rep>
                <rep>

                </rep>
            </choix>

彼は最初に追加します!!!

4

1 に答える 1

3

addChild()DOM に追加されたノードを返すので、その属性で作業を行います

$child = $jeu->situation->question->choix->addChild('rep',"\n".$rep[$i]."\n");
$child->addAttribute('val', $i);
于 2013-11-14T16:24:40.593 に答える