php - Edit SimpleXML node in a foreach loop

309

I'm trying to edit a node value while in a loop. I can edit nodes with unique names just fine.

 $gdNodes->orgName = 'test';

But when I'm in a loop, the value is not saved when I output my XML.

foreach($gdNodes->phoneNumber as $phone)
{
    $phone = '1234567';
}

Both are SimpleXMLElement class objects. I don't understand why it's not saving. How is it done?

137

Answer

Solution:

It won't save because$phone is a scalar copy of the original value.

You should be able to reach your goal like this:

foreach($gdNodes->phoneNumber as $key => $phone)
{
    $gdNodes->phoneNumber[$key] = '1234567';
}

People are also looking for solutions to the problem: Open google map taking the address from PHP variable

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.