У меня есть этот XML:
<picture id="2">
<title>B</title>
</picture>
<picture id="3">
<title>C</title>
</picture>
<picture id="0">
<title>A</title>
</picture>
Пытаясь добиться этого:
<picture id="1">
<title>B</title>
</picture>
<picture id="2">
<title>C</title>
</picture>
<picture id="0">
<title>A</title>
</picture>
Используя это, чтобы получить список значений атрибута 'id':
$objXML = new SimpleXMLElement(XML_FILE_NAME, null, true);
$picture = $objXML->xpath('picture');
$arrayCurrent = array();
foreach($picture as $value) {
$arrayCurrent[] = (string)$value['id'];
}
sort($arrayCurrent); // put XML into numerical 'id' order
print_r($arrayCurrent);
Возвращает: Array ([0] => 0 [1] => 2 [2] => 3)
Любые идеи, как переиндексировать, например, так: 0, 1, 2 и сохранить соответствующие атрибуты 'id' обратно на свои правильные позиции в документе XML?
Спасибо, Энди