在循环之前创建和排序数组[重复]

This question already has an answer here:

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.

</div>

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);