php - 按数组的最高键值获取排序数据
问题描述
我有一个变量 $_SESSION["cart_item"] 它按数组存储购物车项目,如下所示 -
Array
(
[0] => Array
(
[company] => ABC company
[good] => Laptop
[price] => 100
[ship] => 20
)
[1] => Array
(
[company] => ABC company
[good] => PC
[price] => 20
[ship] => 70
)
[2] => Array
(
[company] => DELL PLC
[good] => Hard Drive
[price] => 500
[ship] => 50
)
[3] => Array
(
[company] => ABC company
[good] => Bag
[price] => 30
[ship] => 40
)
)
我想按公司获取排序后的数据,并且[ship]
价值最高。
解决方案
$sortdata = sort2d_bycolumn($data,'ship',SORT_DESC);
function sort2d_bycolumn($array, $column, $method)
{
foreach ($array as $key => $row) {
$narray[$key] = $row[$column];
}
array_multisort($narray, $method, $array);
return $array;
}
sort2d_bycolumn
是排序函数 $array 是您要排序的数组 $column 是需要对数组进行排序的列 $method 值将是SORT_ASC
或者SORT_DESC
推荐阅读
- flutter - 我可以在颤振仪表板中将图标更改为图像或表情符号吗
- python-3.x - 动态规划原始计算器代码优化
- angular - 根据输入在Angular中重新绘制画布线
- android - RecyclerView 滑动删除仍然显示不完整滑动的可绘制
- ip - Google Cloud SQL 上的几个查询
- node.js - 检查电子邮件是否已存在于带有异步和等待的 Mongo Atlas DB 中
- java - 如何将浮动比例转换为长比例?
- python-3.x - 为什么它在结果中显示这个(列表索引超出范围)?
- ios - Xocde - 错误 ITMS-90683 在 Info.plist 文件中缺少目的字符串
- javascript - 达到最大长度值后关注下一个输入 - reactjs