2010-11-22 8 views
1
<root> 
    <gallery name="First"/> 
    <gallery name="Second"/> 
    <gallery name="Third"/> 
</root> 

で複数の属性の名前を変更すると、一度に属性:は、私は複数の "名前" を名前を変更しようとしているのSimpleXML

$rename = array(); 
foreach($_POST['name'] as $value) { 
    $rename[] = $value; 
} 

$objXML = new SimpleXMLElement(XML_FILE_NAME, null, true); 
$gallery = $objXML->xpath('/root/gallery/@name'); 
print_r($gallery); 
print_r($rename); 

$objXML->asXML(XML_FILE_NAME); 

戻り値:

Array ([0] => SimpleXMLElement Object ([@attributes] => Array ([name] => First)) [1] => SimpleXMLElement Object ([@attributes] => Array ([name] => Second)) [2] => SimpleXMLElement Object ([@attributes] => Array ([name] => Third))) 

Array ([0] => First New [1] => Second New [2] => Third New) 

どのように私は新しいを保存するためにPHPを得ることができます値はXMLに戻されますか?別のforeachループが必要ですか?コードはすでに複雑すぎているようだ。

は、私はこれをしようとしているが、ないサイコロは:

foreach($objXML->xpath('/root/gallery/@name') as $gallery) { 
    $gallery = $_POST['name']; 
} 
+0

私ドンsyntaxe

for($i=0, $count=count($aList); $i<$count; $i++) { $a = $aList[$i]; $b = $aList[$i]; var_dump($a==$b); // true } 

だからノードの属性を変更するための唯一の方法は、アレイにある同じインスタンスを返しますどのように '$ rename'がコード内のどのような使用であったかを見てください – stillstanding

+0

これがあなたが探しているものかどうかわかりませんが、 foreachループ内で参照によって変数を使用することができます(実際にはそれを変更します)。あなたのコードには、 'foreach($ objXML-> xpath( '/ root/gallery/@ name')&$ gallery){'。 '$ gallery'に加えられた変更はすべて"スティック "になります。 –

+0

優れています。これは私がすでに上の例で持っているものです。それは私が整理する必要があるループ内のビットです。 – Andy

答えて

1

のSimpleXMLはリターンノードのみにBUIDされません。それは奇妙ですが、'/root/gallery/@name''/root/gallery'です。

これらの2つのクエリ

$aList = $objXML->xpath('/root/gallery/@name'); 
$bList = $objXML->xpath('/root/gallery'); 

foreach($aList as $node) { 
    $node['name'] = 'foo' . $i; 
} 
var_dump($objXML); 
関連する問題