php多维数组排序

时间:2021-09-14 20:57:26

  数组

 array(11) {
[0] => array(7) {
["food_id"] => string(2) "31"
["food_name"] => string(6) "啤酒"
["food_num"] => string(2) "22"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "1"
["x"] => int(2)
}
[1] => array(7) {
["food_id"] => string(2) "32"
["food_name"] => string(6) "红酒"
["food_num"] => string(2) "24"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "2"
["x"] => int(15)
}
[2] => array(7) {
["food_id"] => string(2) "33"
["food_name"] => string(6) "白酒"
["food_num"] => string(2) "12"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "3"
["x"] => int(0)
}
[3] => array(7) {
["food_id"] => string(2) "34"
["food_name"] => string(6) "拉菲"
["food_num"] => string(1) "0"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "4"
["x"] => int(0)
}
[4] => array(7) {
["food_id"] => string(2) "35"
["food_name"] => string(8) "88拉菲"
["food_num"] => string(2) "24"
["foods_id"] => string(1) "8"
["foods_name"] => string(6) "红酒"
["ROW_NUMBER"] => string(1) "5"
["x"] => int(12)
}
[5] => array(7) {
["food_id"] => string(2) "36"
["food_name"] => string(9) "葡萄酒"
["food_num"] => string(1) "0"
["foods_id"] => string(1) "8"
["foods_name"] => string(6) "红酒"
["ROW_NUMBER"] => string(1) "6"
["x"] => int(0)
}
[6] => array(7) {
["food_id"] => string(2) "37"
["food_name"] => string(9) "矿泉水"
["food_num"] => string(1) "0"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "7"
["x"] => int(0)
}
[7] => array(7) {
["food_id"] => string(2) "38"
["food_name"] => string(6) "可乐"
["food_num"] => string(1) "0"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "8"
["x"] => int(0)
}
[8] => array(7) {
["food_id"] => string(2) "39"
["food_name"] => string(6) "雪碧"
["food_num"] => string(2) "50"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "9"
["x"] => int(50)
}
[9] => array(7) {
["food_id"] => string(2) "40"
["food_name"] => string(9) "百岁山"
["food_num"] => string(1) "0"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(2) "10"
["x"] => int(0)
}
[10] => array(7) {
["food_id"] => string(2) "41"
["food_name"] => string(6) "冰霜"
["food_num"] => string(1) "0"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(2) "11"
["x"] => int(0)
}
}

  方法:

 foreach ($food as $key => $row) {
$id[$key] = $row['x'];
}
array_multisort($id, SORT_DESC , $food);

  结果:

 array(11) {
[0] => array(7) {
["food_id"] => string(2) "39"
["food_name"] => string(6) "雪碧"
["food_num"] => string(2) "50"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "9"
["x"] => int(50)
}
[1] => array(7) {
["food_id"] => string(2) "32"
["food_name"] => string(6) "红酒"
["food_num"] => string(2) "24"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "2"
["x"] => int(15)
}
[2] => array(7) {
["food_id"] => string(2) "35"
["food_name"] => string(8) "88拉菲"
["food_num"] => string(2) "24"
["foods_id"] => string(1) "8"
["foods_name"] => string(6) "红酒"
["ROW_NUMBER"] => string(1) "5"
["x"] => int(12)
}
[3] => array(7) {
["food_id"] => string(2) "31"
["food_name"] => string(6) "啤酒"
["food_num"] => string(2) "22"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "1"
["x"] => int(2)
}
[4] => array(7) {
["food_id"] => string(2) "33"
["food_name"] => string(6) "白酒"
["food_num"] => string(2) "12"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "3"
["x"] => int(0)
}
[5] => array(7) {
["food_id"] => string(2) "34"
["food_name"] => string(6) "拉菲"
["food_num"] => string(1) "0"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "4"
["x"] => int(0)
}
[6] => array(7) {
["food_id"] => string(2) "36"
["food_name"] => string(9) "葡萄酒"
["food_num"] => string(1) "0"
["foods_id"] => string(1) "8"
["foods_name"] => string(6) "红酒"
["ROW_NUMBER"] => string(1) "6"
["x"] => int(0)
}
[7] => array(7) {
["food_id"] => string(2) "37"
["food_name"] => string(9) "矿泉水"
["food_num"] => string(1) "0"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "7"
["x"] => int(0)
}
[8] => array(7) {
["food_id"] => string(2) "38"
["food_name"] => string(6) "可乐"
["food_num"] => string(1) "0"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(1) "8"
["x"] => int(0)
}
[9] => array(7) {
["food_id"] => string(2) "40"
["food_name"] => string(9) "百岁山"
["food_num"] => string(1) "0"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(2) "10"
["x"] => int(0)
}
[10] => array(7) {
["food_id"] => string(2) "41"
["food_name"] => string(6) "冰霜"
["food_num"] => string(1) "0"
["foods_id"] => string(1) "1"
["foods_name"] => string(6) "凉菜"
["ROW_NUMBER"] => string(2) "11"
["x"] => int(0)
}
}