For example, the following is the data I read from the database, and then I want to suppliers_id as a benchmark to rearrange the array, what is the better way?
[ { 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", }]
Become
[ //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", } ]]
Reply content:
For example, the following is the data I read from the database, and then I want to suppliers_id as a benchmark to rearrange the array, what is the better way?
[ { 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", }]
Become
[ //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", } ]]
Seeing a label is a php
means of powerlessness.
If it is in front-end processing You can use sort(function(a,b){})
this idea.
Using PHP's Uksort function, this shipment can receive two parameters
$arr = [{c:"213"},{c:"dadad"}]function compare_func($a, $b){ return $a=>c - $b=>c;}uksrot($arr,"compare_func" );