NobleUplift
NobleUplift

Reputation: 6024

How do I set an existing SimpleXMLElement attribute?

I am running the following code:

$attributes = $xmlTable2->attributes();
foreach ($attributes as $key => $value) {
    $xmlTable1->addAttribute($key, $value);
}

But it causes an error:

Warning: SimpleXMLElement::addAttribute(): Attribute already exists in ... on line 100
Buildfile: /.../vendor/propel/propel1/generator/build.xml

How do I override the value of an existing attribute?

Upvotes: 1

Views: 2044

Answers (1)

Zarathuztra
Zarathuztra

Reputation: 3251

Easiest way is to just use the attributes() function that is part of the SimpleXML object on the element you want to get the attributes for.

$xmlTable1->elementYouWantAttrsFor->attributes()->attrName = newAttrValue;

or if each $xmlTable1 element is what you want

$xmlTable1->attributes()->attrName = newAttrValue;

Here is the documentation. The comments show how to do this.

Upvotes: 2

Related Questions