0

I need to create an array from XML response and then order by one of the existing XML field.

I have parsed the XML fine and ended up with 3 values that I need in my foreach loop.

$optionsArray = array();

foreach ($options as $key => $option) {
  $price = $option->Price;
  $shortDesc = $option->ShortDescription;
  $longDesc = $option->LongDescription;

  $optionsArray[] = array('shortdesc' => $shortDesc, 'longdesc' => $longDesc, 'price' => $price);
}

This works fine, but now I wish to order the array using the 'price' value (descending) and then I can show the items correctly.

I have looked into usort and arsort and all the others but cannot make sense. Any examples using my code for help?

Thanks.

Swatantra Kumar
  • 1,161
  • 5
  • 24
  • 29
Lovelock
  • 6,609
  • 15
  • 71
  • 162
  • check this answer :-http://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php – Ranjeet Singh Dec 19 '14 at 10:15

1 Answers1

1

Should do the trick

$sortArr = array();
$optionsArray = array();
foreach ($options as $key => $option) {
    $price = $option->Price;
    $shortDesc = $option->ShortDescription;
    $longDesc = $option->LongDescription;

    $optionsArray[] = array('shortdesc' => $shortDesc, 'longdesc' => $longDesc, 'price' => $price);
    $sortArr[] = $price;
}

array_multisort($sortArr, SORT_ASC, $optionsArray);
Brain Foo Long
  • 1,914
  • 18
  • 24