2014-12-19 1 views
0

Я пытаюсь использовать array_multisort(), но он работает неправильно.PHP array_multisort не работает правильно

Я 2 массивы установить:

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

Я тогда цель сортировать $ optionsArray используя цену, выполнив:

array_multisort($sortArr, SORT_DESC, $optionsArray); 

Вопрос заключается в том, что порядок результатов сделать изменения , но не правильно. Пример, который я использовал, на самом деле довольно близок, но другие нигде не близки.

Когда вар демпинг массивы после использования 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

Вы пытаетесь отсортировать 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; 
    } 
} 
+0

совершенным, спасибо! Знал, что это было что-то в этом роде. – Lovelock