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

1

3 Answers 3

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

mmm, have you had a look at the comparision functions in it. link
you are right I could do it with: sort($urlarray, SORT_STRING );
Great glad I could help
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,

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.