English 中文(简体)
PHP 简单易变
原标题:PHP simplexml reverse

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

不管用什么方式来扭转xml美元,这样一来,如果是这样的话,就算项目3。

问题回答

假设采用“条码”()建议采用的解决办法一(d)

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

注:XPath的“项目”包含所有<代码>和>;项目/项目;中属于目前要素的直接儿童的内容。

$items = $xml->item;
for ($i = count($items) - 1; $i >= 0; $i--)
{
    echo (string) $items[$i].", ";
}

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];
}

印刷:

321

Another expample:

它使用较强的。 如果你真的想要的话,你会这样做;

<?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

这对我来说是罚款的:

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

Iterators 载于PHP没有previous 风格方法,答案是没有的。 你们可以把它们读成一个阵列,然后扭转阵列,但这不会直接使后退。

一种选择是建立一个反转录器,把一个固定的储存器包起来,然后向后背。 但是,这可能会产生问题,因为你不知道一台拖车是否结束(见一些例子,没有在

那么,你就不能说。 你的唯一选择是做这样的事情:

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

<代码>xml->>项目为阵列。 使用阵列功能。

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

EDIT:O,没有办法这样做。 你们首先必须做一个阵列。

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

(无检测)





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签