I am in need of help to figure out something.
我需要帮助来找出一些东西。
On my last question I was talking about parsing a XML and listing the values on html: Parse XML to HTML with PHP SimpleXMLElement
在我的上一个问题上,我在讨论解析XML并在html上列出值:使用PHP SimpleXMLElement将XML解析为HTML
I got that going really well, but then a new variable presented itself. :)
我做得很好,但随后出现了一个新的变量。 :)
This is my XML:
这是我的XML:
<?xml version='1.0'?>
<AdXML>
<Response>
<Campaign>
<Overview>
<Name>strip</Name>
<Description>category</Description>
<Status>L</Status>
</Overview>
<Pages>
<Url>page01</Url>
<Url>page02</Url>
<Url>page03</Url>
</Pages>
</Campaign>
</Response>
</AdXML>
My problem start when my XML generate the page's list random.so, one time it would load like this and another time it will load like this:
我的问题开始于我的XML生成页面的列表random.so,有一次它会像这样加载,另一次它会像这样加载:
<?xml version='1.0'?>
<AdXML>
<Response>
<Campaign>
<Overview>
<Name>strip</Name>
<Description>category</Description>
<Status>L</Status>
</Overview>
<Pages>
<Url>page02</Url>
<Url>page03</Url>
<Url>page01</Url>
</Pages>
</Campaign>
</Response>
</AdXML>
So I went from a simple:
所以我从一个简单的:
<?php foreach ($xmlparsed->Response->Campaign->Pages->Url as $Url) {echo $Url, '<br>';} ?>
to:
<?php
$urlarray = array();
foreach ($xmlparsed->Response->Campaign->Pages->Url as $Url) {$urlarray[] = $Url;}
sort($urlarray);
foreach ($urlarray as $key => $val){echo $key,'|',$val, '<br>';}
?>
What I am trying to do is to create an array with the information and then sort the pages alphabetically. I do not understand why cant I sort by value (pages), I can only sort by key and that defeat the purpose just because the xml is generated dynamically and I have no control on how it is formed.
我想要做的是创建一个包含信息的数组,然后按字母顺序对页面进行排序。我不明白为什么我不能按值(页面)排序,我只能按键排序并且因为xml是动态生成而无法控制它的形成而失败了目的。
ay help would be greatly appreciated .
非常感谢你的帮助。
cheers
3 个解决方案
#1
0
Look at asort
and similar functions in PHP, there are a few that will help.
看看PHP中的asort和类似函数,有一些会有所帮助。
#2
0
This is the case of customized sorting array, in this case I suggest using usort:
这是自定义排序数组的情况,在这种情况下我建议使用usort:
For PHP version >= 5.4:
对于PHP版本> = 5.4:
usort($urlarray, function ($a, $b) {
if ((int)substr($a, 4, 2) > (int)substr($b, 4, 2)) return 1;
elseif ((int)substr($a, 4, 2) < (int)substr($b, 4, 2)) return -1;
else return 0;
});
For PHP version < 5.4:
对于PHP版本<5.4:
function custom_sort($a, $b) {
if ((int)substr($a, 4, 2) > (int)substr($b, 4, 2)) return 1;
elseif ((int)substr($a, 4, 2) < (int)substr($b, 4, 2)) return -1;
else return 0;
}
// then using in usort
usort($urlarray, 'custom_sort');
Hope this help. Regards,
希望这有帮助。问候,
#3
0
I was able to do it using a SORT_STRING:
我能够使用SORT_STRING来做到这一点:
<?php
$urlarray = array();
foreach ($xmlparsed->Response->Campaign->Pages->Url as $Url) {$urlarray[] = $Url;}
sort($urlarray, SORT_STRING);
foreach ($urlarray as $key => $val){echo $key,'|',$val, '<br>';}
?>
thank you all
谢谢你们
#1
0
Look at asort
and similar functions in PHP, there are a few that will help.
看看PHP中的asort和类似函数,有一些会有所帮助。
#2
0
This is the case of customized sorting array, in this case I suggest using usort:
这是自定义排序数组的情况,在这种情况下我建议使用usort:
For PHP version >= 5.4:
对于PHP版本> = 5.4:
usort($urlarray, function ($a, $b) {
if ((int)substr($a, 4, 2) > (int)substr($b, 4, 2)) return 1;
elseif ((int)substr($a, 4, 2) < (int)substr($b, 4, 2)) return -1;
else return 0;
});
For PHP version < 5.4:
对于PHP版本<5.4:
function custom_sort($a, $b) {
if ((int)substr($a, 4, 2) > (int)substr($b, 4, 2)) return 1;
elseif ((int)substr($a, 4, 2) < (int)substr($b, 4, 2)) return -1;
else return 0;
}
// then using in usort
usort($urlarray, 'custom_sort');
Hope this help. Regards,
希望这有帮助。问候,
#3
0
I was able to do it using a SORT_STRING:
我能够使用SORT_STRING来做到这一点:
<?php
$urlarray = array();
foreach ($xmlparsed->Response->Campaign->Pages->Url as $Url) {$urlarray[] = $Url;}
sort($urlarray, SORT_STRING);
foreach ($urlarray as $key => $val){echo $key,'|',$val, '<br>';}
?>
thank you all
谢谢你们