1

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

I got that going really well, but then a new variable presented itself. :)

This is my 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 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.

ay help would be greatly appreciated .

cheers

Community
  • 1
  • 1
Paulo Capelo
  • 1,062
  • 1
  • 9
  • 13
  • might try it like this: http://stackoverflow.com/questions/15604459/sorting-the-table-fields-using-simple-xml-and-xpath/15625514#15625514 – michi Oct 16 '15 at 15:45

3 Answers3

0

Look at asort and similar functions in PHP, there are a few that will help.

Richard Housham
  • 1,525
  • 2
  • 15
  • 31
0

This is the case of customized sorting array, in this case I suggest using usort:

For PHP version >= 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:

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,

Dat Pham
  • 1,765
  • 15
  • 13
0

I was able to do it using a 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

Paulo Capelo
  • 1,062
  • 1
  • 9
  • 13