PHP simplexml 反向

发布于 2024-09-15 10:30:58 字数 294 浏览 10 评论 0原文

有没有办法反向循环 simpleXML 中加载的 xml? 假设我有以下 xml

$string ="<items>
    <item>1</item>
    <item>2</item>
    <item>3</item> 
</items>";

$xml = simplexml_load_string($string);

有没有办法反转 $xml 以便我在执行 foreach 时得到第 3 项

Is there any way to loop through an xml loaded in simpleXML in reverse?
say if i have the following xml

$string ="<items>
    <item>1</item>
    <item>2</item>
    <item>3</item> 
</items>";

$xml = simplexml_load_string($string);

is there any way to reverse $xml so that i get item 3 when i do a foreach

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

︶ ̄淡然 2024-09-22 10:30:58

假设OP是一个初学者,只对解决方案感兴趣,我建议使用xpath()来获取元素,并使用array_reverse()来反转它们的顺序:

$items = array_reverse($xml->xpath('item'));

注意:XPath “item” 获取当前元素的直接子元素的所有 元素。

Assuming the OP is a beginner only interested in a solution I'd recommend using xpath() to get the elements and array_reverse() to reverse their order:

$items = array_reverse($xml->xpath('item'));

Note: the XPath "item" grabs all <item/> elements that are the direct children of current element.

幸福%小乖 2024-09-22 10:30:58
$items = $xml->item;
for ($i = count($items) - 1; $i >= 0; $i--)
{
    echo (string) $items[$i].", ";
}
$items = $xml->item;
for ($i = count($items) - 1; $i >= 0; $i--)
{
    echo (string) $items[$i].", ";
}
美羊羊 2024-09-22 10:30:58

使用 simplexml:

<?php
$string ="<items>
    <item>1</item>
    <item>2</item>
    <item>3</item>
</items>";

$xml = simplexml_load_string($string);

$length = count($xml->item);
for($i = $length; $i; --$i) {
    echo $xml->item[$i-1];
}

打印:

321

另一个例子:

它有点冗长,并且使用了更强大的 DomDocument如果您确实想要,则不是 SimpleXml ;)

<?php
$string ="<items>
    <item>1</item>
    <item>2</item>
    <item>3</item>
</items>";

$oXml = DomDocument::loadXml($string);

$oItems = $oXml->firstChild->childNodes;
$lastItemIndex = $oItems->length;

$oItem = $oItems->item($lastItemIndex-1);
do {
    echo $oItem->nodeValue;
} while($oItem = $oItem->previousSibling);

打印:

3
2
1

With simplexml:

<?php
$string ="<items>
    <item>1</item>
    <item>2</item>
    <item>3</item>
</items>";

$xml = simplexml_load_string($string);

$length = count($xml->item);
for($i = $length; $i; --$i) {
    echo $xml->item[$i-1];
}

prints:

321

Another expample:

It's a little verbose and it uses the more powerful DomDocument and not SimpleXml if you really want to ;)

<?php
$string ="<items>
    <item>1</item>
    <item>2</item>
    <item>3</item>
</items>";

$oXml = DomDocument::loadXml($string);

$oItems = $oXml->firstChild->childNodes;
$lastItemIndex = $oItems->length;

$oItem = $oItems->item($lastItemIndex-1);
do {
    echo $oItem->nodeValue;
} while($oItem = $oItem->previousSibling);

prints:

3
2
1
平生欢 2024-09-22 10:30:58

这对我来说效果很好:

$string = "<items>
<item>1</item>
<item>2</item>
<item>3</item>
</items>";
$xml = simplexml_load_string($string); 
$reverseArray = (array) $xml;
$reverseArray = array_reverse($reverseArray["item"]);
print_r($reverseArray);

输出:

Array
(
    [0] => 3
    [1] => 2
    [2] => 1
)

This works fine for me:

$string = "<items>
<item>1</item>
<item>2</item>
<item>3</item>
</items>";
$xml = simplexml_load_string($string); 
$reverseArray = (array) $xml;
$reverseArray = array_reverse($reverseArray["item"]);
print_r($reverseArray);

Output:

Array
(
    [0] => 3
    [1] => 2
    [2] => 1
)
韵柒 2024-09-22 10:30:58

由于 PHP 中的 迭代器 没有 < code>previous 风格的方法,简短的答案是否定的。您可以将它们读入数组,然后反转数组,但这不会直接向后迭代。

一种选择是创建一个 ReverseIterator,在其中包装一个常规迭代器,然后向后遍历它。但这可能会出现问题,因为您不知道迭代器是否有结束(您可以在 SPL)。

所以不,你不能。你唯一的选择是做这样的事情:

$nodes = array();
foreach ($xml AS $node) {
    array_unshift($nodes, $node);
}

Since Iterators in PHP have no previous style method, the short answer is no. You could read them into an array and then reverse the array, but that wouldn't be directly iterating backwards.

One option would be to create a ReverseIterator, where you wrap a regular iterator and then traverse it backwards. But that might get problematic since you don't know if a iterator has an end (You can see some examples that don't in the SPL).

So no, you can't. Your only option would be to do something like this:

$nodes = array();
foreach ($xml AS $node) {
    array_unshift($nodes, $node);
}
べ繥欢鉨o。 2024-09-22 10:30:58

$xml->item 是一个数组。使用数组排序函数。

rsort($xml->item);
print_r($xml->item);

编辑:好的,没有办法做到这一点。你必须先创建一个数组。

$myArray = array_reverse((array)$xml->item, true);
print_r($myArray);

(不再测试)

$xml->item is an array. Use array sorting functions.

rsort($xml->item);
print_r($xml->item);

EDIT: Ok, there is no way to do this. You have to make an array first.

$myArray = array_reverse((array)$xml->item, true);
print_r($myArray);

(no testing again)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文