tags:

views:

54

answers:

2
+2  A: 

1.Get them from XML (you seem to be able to do that).
2.Put them in an array
3.sort() the array, if your items are more complex. possibly using usort():

$d =  new SimpleXMLElement('<root>
  <item>
    <price>1234</price>
  </item>
  <item>
    <price>4123</price>
  </item>
  <item>
    <price>3142</price>
  </item>
  <item>
    <price>2431</price>
  </item>
</root>');
$items = array();
foreach($d->item as $item) $items[] = $item;
function _mysort($a,$b){return $a->price - $b->price;}
usort($items,'_mysort');
var_dump($items);
Wrikken
I am getting this:Warning: sort() expects parameter 1 to be array, null given in /path on line 315I am using the following code:$all = new SimpleXMLElement($url, null, true);$precio = ($hotel->minCostOfStay); function _mypricesorter($a,$b){ return $a->minCostOfStay - $b->minCostOfStay; }sort($precio,'_mypricesorter'); ideas?
Flavio
`$hotel->minCostOfStay` is not an array. I added an example to the answer.
Wrikken
can we go back to what we had and start from there, I think that method is easier for me. As you can see (info updated in the question), am alredy using a foreach and I am not sure on how to use a foreach inside a foreach.. It is possible?
Flavio
Ok Wrikken, your example works. Thing is how do I extract the info out from the XML and write it where the <root><item><price>1234</price></item> is?
Flavio
+1  A: 

I recently disocvered SimpleDOM for a project where I needed a more advanced xpath system. I really like it and I think it could do what you're looking for. You can find it (docs are bundled in the source code download) here: http://code.google.com/p/simpledom/

You'll want to look at the sortedXPath() method I believe:

sortedXPath (line 888)

Run an XPath query and sort the result

This method accepts any number of arguments in a way similar to array_multisort()

   1. // Retrieve all <x/> nodes, sorted by @foo ascending, @bar descending
   2. $root->sortedXPath('//x', '@foo', '@bar', SORT_DESC);
   3. 
   4. // Same, but sort @foo numerically and @bar as strings
   5. $root->sortedXPath('//x', '@foo', SORT_NUMERIC, '@bar', SORT_STRING, SORT_DESC);

    * access: public

void sortedXPath (string $xpath)

    * string $xpath: XPath expression