比如下面是我从数据库中读取出来的数据,然后我想以suppliers_id为基准重新排列这个数组,有什么比较好的方法?
<code>[
{
cart_id: "6",
suppliers_id: "1",
goods_name: "商品1",
},
{
cart_id: "7",
suppliers_id: "1",
goods_name: "商品2",
}
{
cart_id: "7",
suppliers_id: "1",
goods_name: "商品3",
}
]</code>变成
<code>[
//suppliers_id
1:
[
{
cart_id: "6",
suppliers_id: "1",
goods_name: "商品1",
},
{
cart_id: "7",
suppliers_id: "1",
goods_name: "商品3",
}
]
//suppliers_id
2:
[
{
cart_id: "7",
suppliers_id: "1",
goods_name: "商品2",
}
]
]</code>比如下面是我从数据库中读取出来的数据,然后我想以suppliers_id为基准重新排列这个数组,有什么比较好的方法?
<code>[
{
cart_id: "6",
suppliers_id: "1",
goods_name: "商品1",
},
{
cart_id: "7",
suppliers_id: "1",
goods_name: "商品2",
}
{
cart_id: "7",
suppliers_id: "1",
goods_name: "商品3",
}
]</code>变成
<code>[
//suppliers_id
1:
[
{
cart_id: "6",
suppliers_id: "1",
goods_name: "商品1",
},
{
cart_id: "7",
suppliers_id: "1",
goods_name: "商品3",
}
]
//suppliers_id
2:
[
{
cart_id: "7",
suppliers_id: "1",
goods_name: "商品2",
}
]
]</code>
看到标签是 php 表示无能为力。
如果是在前端处理你倒是可以用 sort(function(a,b){}) 这种思路来。
使用PHP的uksort函数,这货可以接收两个参数
<code>$arr = [
{c:"213"}
,
{c:"dadad"}]
function compare_func($a, $b){
return $a=>c - $b=>c;
}
uksrot($arr,"compare_func" );</code>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号