I have been playing with the array_multisort function but I am struggling to get it to work with the array that I have.
Here is a var dump of the multi dimensional array I am trying to sort:
array(2) {
[1]=> array(6) {
["totalprice"]=> float(103.32)
["itemsprice"]=> float(83.33)
["deliveryprice"]=> float(19.99)
["qualityscore"]=> int(100)
["reliabilityscore"]=> int(100)
["itemtemplates"]=> array(4) {
[1]=> array(3) {
["price"]=> float(374)
["qty"]=> int(200)
["name"]=> string(17) "English A2 Poster"
}
[3]=> array(3) {
["price"]=> float(374)
["qty"]=> int(500)
["name"]=> NULL
}
[6]=> array(3) {
["price"]=> float(83.333333333333)
["qty"]=> int(100)
["name"]=> string(16) "French A3 Poster"
}
[5]=> array(3) {
["price"]=> float(83.333333333333)
["qty"]=> int(5000) ["name"]=> NULL
}
}
}
[2]=> array(6) {
["totalprice"]=> float(103.32)
["itemsprice"]=> float(83.33)
["deliveryprice"]=> float(19.99)
["qualityscore"]=> int(80)
["reliabilityscore"]=> int(100)
["itemtemplates"]=> array(4) {
[1]=> array(3) {
["price"]=> float(374)
["qty"]=> int(200)
["name"]=> string(17) "English A2 Poster"
}
[3]=> array(3) {
["price"]=> float(374)
["qty"]=> int(500)
["name"]=> NULL
}
[6]=> array(3) {
["price"]=> float(83.333333333333)
["qty"]=> int(100)
["name"]=> string(16) "French A3 Poster"
}
[5]=> array(3) {
["price"]=> float(83.333333333333)
["qty"]=> int(5000) ["name"]=> NULL
}
}
}
[3]=> array(6) {
["totalprice"]=> float(83.32)
["itemsprice"]=> float(63.33)
["deliveryprice"]=> float(19.99)
["qualityscore"]=> int(60)
["reliabilityscore"]=> int(40)
["itemtemplates"]=> array(4) {
[1]=> array(3) {
["price"]=> float(374)
["qty"]=> int(200)
["name"]=> string(17) "English A2 Poster"
}
[3]=> array(3) {
["price"]=> float(374)
["qty"]=> int(500)
["name"]=> NULL
}
[6]=> array(3) {
["price"]=> float(83.333333333333)
["qty"]=> int(100)
["name"]=> string(16) "French A3 Poster"
}
[5]=> array(3) {
["price"]=> float(83.333333333333)
["qty"]=> int(5000) ["name"]=> NULL
}
}
}
}
I need to sort by the total price ASC and then by the quality score DESC.
I’ve tried the following:
$sorted = array_multisort($array['totalprice'], SORT_ASC, SORT_NUMERIC,
$array['qualityscore'], SORT_NUMERIC, SORT_DESC);
Unfortunately that doesn’t work. Is anyone a bit more savvy with this function and may know where i’m going wrong? or if there is a simple alternative to this function?
Thanks in Advance!
Try following code:
RTM http://php.net/manual/en/function.array-multisort.php 🙂