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

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 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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站