Reputation: 21
These 2 lines below currently work within the XML document to produce the 2 values :
if (isset($images[0]->fname)) { $image1 = $theimgpath.'/'.$images[0]->fname; } else { $image1 = ''; }
if (isset($images[1]->fname)) { $image2 = $theimgpath.'/'.$images[1]->fname; } else { $image2 = ''; }
$image1 current value working = url within xml document require assignment to <image id="1">
$image2 current value = url working url within xml document require assignment to <image id="2">
$output .= "<url>".$image1."</url>\n"; example of current working value
Desired outcome the above working in code snippet currently working in the same xmldocument below:
`$output .= "<property>\n";
$id = $xml["id"];
$id = $xml->image['id'];
$output .= $string = <<<XML
<images>
<image id="1"><url>id”1”</url></image>
<image id="2"><url>id”2”</url></image>
</images>
XML;
$output .= "</property>";
}
$output .= "</root>";`
Upvotes: 0
Views: 1818
Reputation: 234
SimpleXML library have method addAttribute
$image1->addAttribute('id', '1');
$image2->addAttribute('id', '2');
But I recomend to use DomDocument to work with xml it is much stronger
This is a working example of what you need
<?php
$xml = <<<XML
<images>
<image><url>id"1"</url></image>
<image><url>id"2"</url></image>
</images>
XML;
$images = simplexml_load_string($xml);
$images->image[0]->addAttribute('id',1);
$images->image[1]->addAttribute('id',2);
echo $images->asXML();
?>
Upvotes: 1