drlndkhib08556095
drlndkhib08556095
2014-12-19 12:26
浏览 49
已采纳

PHP array_multisort无法正常工作

I have been trying to use array_multisort() but its not working correctly.

I have 2 arrays set up:

$sortArr = array(); // array used for sorting by price
$optionsArray = array();

I then aim to sort the $optionsArray using the price by doing:

array_multisort($sortArr, SORT_DESC, $optionsArray);

The issue is that the order of the results do change, but not correctly. The example I have used is actually quite close but others are nowhere near.

When var dumping the arrays AFTER using array_multisort I get:

($sortArr):

 array(8) { 
   [0]=> object(SimpleXMLElement)#61 (1) { [0]=> string(3) "610" } 
   [1]=> object(SimpleXMLElement)#66 (1) { [0]=> string(3) "300" } 
   [2]=> object(SimpleXMLElement)#71 (1) { [0]=> string(3) "235" } 
   [3]=> object(SimpleXMLElement)#56 (1) { [0]=> string(1) "0" } 
   [4]=> object(SimpleXMLElement)#51 (1) { [0]=> string(3) "135" } 
   [5]=> object(SimpleXMLElement)#41 (1) { [0]=> string(1) "0" } 
   [6]=> object(SimpleXMLElement)#46 (1) { [0]=> string(1) "0" } 
   [7]=> object(SimpleXMLElement)#36 (1) { [0]=> string(1) "0" } 
 }

($optionsArray):

 array(8) { 
   [0]=> array(4) { 
      ["shortdesc"]=> object(SimpleXMLElement)#60 (1) { 0]=> string(14) "Metallic paint" } 
      ["longdesc"]=> object(SimpleXMLElement)#54 (1) { [0]=> string(14) "Metallic paint" } 
      ["price"]=> object(SimpleXMLElement)#61 (1) { [0]=> string(3) "610" } 
      ["kind"]=> object(SimpleXMLElement)#62 (1) { [0]=> string(8) "Optional" } } 
   [1]=> array(4) { 
      ["shortdesc"]=> object(SimpleXMLElement)#65 (1) { [0]=> string(43) "Seat heating for driver and front passenger" } 
      ["longdesc"]=> object(SimpleXMLElement)#59 (1) { [0]=> string(33) "Driver and passenger seat: heated" } 
      ["price"]=> object(SimpleXMLElement)#66 (1) { [0]=> string(3) "300" } 
      ["kind"]=> object(SimpleXMLElement)#67 (1) { [0]=> string(8) "Optional" } } 
   [2]=> array(4) { 
      ["shortdesc"]=> object(SimpleXMLElement)#70 (1) { [0]=> string(20) "Sun protection glass" } 
      ["longdesc"]=> object(SimpleXMLElement)#64 (1) { [0]=> string(61) "Privacy glass on the rear window and on the rear side windows" } 
      ["price"]=> object(SimpleXMLElement)#71 (1) { [0]=> string(3) "235" } 
      ["kind"]=> object(SimpleXMLElement)#72 (1) { [0]=> string(8) "Optional" } } 
   [3]=> array(4) { 
      ["shortdesc"]=> object(SimpleXMLElement)#55 (1) { [0]=> string(23) "Glacier Silver metallic" } 
      ["longdesc"]=> object(SimpleXMLElement)#49 (1) { [0]=> string(23) "External colour: silver" } 
      ["price"]=> object(SimpleXMLElement)#56 (1) { [0]=> string(1) "0" } 
      ["kind"]=> object(SimpleXMLElement)#57 (1) { [0]=> string(8) "Optional" } } 
   [4]=> array(4) { 
      ["shortdesc"]=> object(SimpleXMLElement)#50 (1) { [0]=> string(16) "Extended storage" } 
      ["longdesc"]=> object(SimpleXMLElement)#44 (1) { [0]=> string(243) "2 x 12v power outlet located in rear section, Seat back storage: pockets behind front seats, Versatile net, Storage net on left in luggage compartment, Two extra lashing eyes in luggage compartment, Retaining strap on right luggage compartment" } 
      ["price"]=> object(SimpleXMLElement)#51 (1) { [0]=> string(3) "135" } 
      ["kind"]=> object(SimpleXMLElement)#52 (1) { [0]=> string(8) "Optional" } } 
   [5]=> array(4) { 
      ["shortdesc"]=> object(SimpleXMLElement)#40 (1) { [0]=> string(31) "Brushed Aluminium interior trim" } 
      ["longdesc"]=> object(SimpleXMLElement)#29 (1) { [0]=> string(109) "Alloy look trim on dashboard, alloy look trim on doors and alloy look trim on centre console, Alloy dashboard" } 
      ["price"]=> object(SimpleXMLElement)#41 (1) { [0]=> string(1) "0" } 
      ["kind"]=> object(SimpleXMLElement)#42 (1) { [0]=> string(8) "Optional" } } 
   [6]=> array(4) { 
      ["shortdesc"]=> object(SimpleXMLElement)#45 (1) { [0]=> string(22) "Dakota Leather - Black" } 
      ["longdesc"]=> object(SimpleXMLElement)#39 (1) { [0]=> string(61) "Leather and leather seat upholstery, Upholstery colour: black" } 
      ["price"]=> object(SimpleXMLElement)#46 (1) { [0]=> string(1) "0" } 
      ["kind"]=> object(SimpleXMLElement)#47 (1) { [0]=> string(8) "Optional" } } 
   [7]=> array(4) { 
      ["shortdesc"]=> object(SimpleXMLElement)#35 (1) { [0]=> string(17) "Brushed Aluminium" } 
      ["longdesc"]=> object(SimpleXMLElement)#34 (1) { [0]=> string(77) "Alloy trim on dashboard, alloy trim on doors and alloy trim on centre console" } 
      ["price"]=> object(SimpleXMLElement)#36 (1) { [0]=> string(1) "0" } 
      ["kind"]=> object(SimpleXMLElement)#37 (1) { [0]=> string(8) "Optional" } } 
  }

The arrays are created using this:

foreach ($options as $key => $option) { // create the array for the options
  if ($option->OptionKind != 'Standard') {
    $longDesc = $option->LongDescription;
    $shortDesc = $option->ShortDescription;
    $optionPrice = $option->Price;
    $optionType = $option->OptionKind;
    $optionsArray[] = array('shortdesc' => $shortDesc, 'longdesc' => $longDesc, 'price' => $optionPrice, 'kind' => $optionType);
    $sortArr[] = $optionPrice;
    }
}

Any ideas where I am going wrong?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douding_1073
    douding_1073 2014-12-19 12:31
    已采纳

    You're trying to sort SimpleXMLElement objects, not strings. Try casting the values to strings before you add them to the array:

    foreach ($options as $key => $option) { // create the array for the options
      if ($option->OptionKind != 'Standard') {
        $longDesc = (string) $option->LongDescription;
        $shortDesc = (string) $option->ShortDescription;
        $optionPrice = (string) $option->Price;
        $optionType = (string) $option->OptionKind;
        $optionsArray[] = array('shortdesc' => $shortDesc, 'longdesc' => $longDesc, 'price' => $optionPrice, 'kind' => $optionType);
        $sortArr[] = $optionPrice;
        }
    }
    
    点赞 评论

相关推荐