PHP XML简单变量问题

I would like to change the 4c050652f0c3e ( of <testimonial id="xxx"> ) to a dynamic variable that it is sent through a form. My variable is $nodeid = $_POST['nodeid']; but i can not replace it right.

This is the code

foreach( $testimonials->xpath("testimonial[@id='4c050652f0c3e']") as $t ) {
  $t->$_POST['tagname'] = $_POST['newname'];
}

This is what I did but it is not right

foreach( $testimonials->xpath("testimonial[@id=$_POST['nodeid']]") as $t ) {
  $t->$_POST['tagname'] = $_POST['newname'];
}

Thank you!

try add '

foreach( $testimonials->xpath("testimonial[@id='".$_POST['nodeid']]."'") as $t ) {
  $t->$_POST['tagname'] = $_POST['newname'];
}

foreach works on a copy of your array, so this would not work. You could use a different loop (for loop?), or use references to the array

Try this:

foreach( $testimonials->xpath("testimonial[@id='".$_POST['nodeid']."']") as $t ) {
  $t->$_POST['tagname'] = $_POST['newname'];
}