1

Possible Duplicate:
Sorting xml with simpleXML/xpath?

I'm using SimpleXML and I'm trying to sort my list by the amount of views it has had. I have a generic foreach function that runs after the sorting as below:

    if($filter=="popular"){
        $sort=$x->xpath('//example[views]');
        function cmp($a, $b){
              if ( $a->id > $b->id) return -1;
              if ( $a->id < $b->id) return 1;
              return 0;
            }
        usort($sort, cmp); //this isn't working?
    } else {
        $sort = $x->example;
    }

    foreach($sort as $q){ ...

My XML looks something like:

    <list>
        <example>
            <id>1</id>
            <name>bob<name>
            <views>5</views>
        </example>
        <example>
            <id>2</id>
            <name>fred<name>
            <views>18</views>
    </example>
    <example>
        <id>3</id>
        <name>alfie<name>
        <views>0</views>
    </example>
</list>

So I just need a way to sort by the amount of views each example has had in DESC order - highest views to lowest views.

Any ideas?

Many thanks :)

Community
  • 1
  • 1
Tim
  • 6,986
  • 8
  • 38
  • 57

1 Answers1

0

That would be because you are using the function name as a constant and need to use it as a string like this: usort($sort, 'cmp'); instead of usort($sort, cmp);

Catalin
  • 858
  • 5
  • 16