重新排序阵列属性然后再保存到PHP的XML

编程入门 行业动态 更新时间:2024-10-24 20:19:11
本文介绍了重新排序阵列属性然后再保存到PHP的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这样的XML:

<picture id="2"> <title>B</title> </picture> <picture id="3"> <title>C</title> </picture> <picture id="0"> <title>A</title> </picture>

努力实现这一点:

Trying to achieve this:

<picture id="1"> <title>B</title> </picture> <picture id="2"> <title>C</title> </picture> <picture id="0"> <title>A</title> </picture>

使用该得到的id属性值的列表:

Using this to get a list of 'id' attribute values:

$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);

它返回:阵列([0] => 0 [1] => 2 [2] => 3)任何想法如何重新建立索引像这样:0,1,2并保存相应的'ID'在XML文档属性回到正确的位置。

It returns: Array ( [0] => 0 [1] => 2 [2] => 3 ) Any ideas how to re-index like so: 0, 1, 2 and save the appropriate 'id' attributes back to their correct positions in the XML doc?

谢谢,安迪

推荐答案

数字ID为了保持并取得连续的:

Numerical id order is maintained and made consecutive:

$objXML = new SimpleXMLElement(XML_FILE_NAME, null, true); $picture = $objXML->xpath('picture'); usort($picture, create_function('$a,$b', 'return (string)$a["id"] - (string)$b["id"];')); foreach ($pictures as $index => $node) $node["id"] = $index;

更多推荐

重新排序阵列属性然后再保存到PHP的XML

本文发布于:2023-11-27 18:36:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639139.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:然后再   阵列   属性   XML   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!