PHP array_multisort无法正常工作

时间:2014-12-19 12:26:31

标签: php arrays foreach array-multisort

我一直在尝试使用array_multisort(),但它无法正常工作。

我设置了2个阵列:

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

然后我的目标是通过以下方式使用价格对$ optionsArray进行排序:

array_multisort($sortArr, SORT_DESC, $optionsArray);

问题是结果的顺序确实发生了变化,但不正确。我使用的例子实际上非常接近,但其他人远远不够。

当var使用array_multisort转储数组后,我得到:

($ 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" } } 
  }

使用以下方法创建数组:

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

我出错的任何想法?

1 个答案:

答案 0 :(得分:1)

您正在尝试对SimpleXMLElement个对象进行排序,而不是字符串。在将值添加到数组之前,尝试将值转换为字符串:

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