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,324
  • 5
  • 24
  • 32
Lovelock
  • 7,689
  • 19
  • 86
  • 186
  • 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
  • 2,057
  • 23
  • 28