在我的回复中我得到
[ { "id": 480, "HSN": 4808, }, { "id": 575, "HSN": 5702, }, { "id": 638, "HSN": 6301, }, { "id": 765, "HSN": 7317, }, { "id": 6626, "HSN": 47071000, }, { "id": 6727, "HSN": 48081000, }, { "id": 6728, "HSN": 48084010, } ]
但我希望我的回复以某种方式排序,它检查 HSN(基于 HSN 的前 2 位数字)并给我这样的结果
[ { "id": 6626, "HSN": 47071000, }, { "id": 480, "HSN": 4808, }, { "id": 6727, "HSN": 48081000, }, { "id": 6728, "HSN": 48084010, }, { "id": 575, "HSN": 5702, }, { "id": 638, "HSN": 6301, }, { "id": 765, "HSN": 7317, } ]
我尝试过 sortBy 、排序方法,但它似乎对我不起作用,或者我以错误的方式执行此操作。 帮助我以正确的方式得到它
P粉4500792662023-09-09 10:42:03
$data = [ [ "id" => 480, "HSN" => 4808, ], [ "id"=> 575, "HSN"=> 5702, ], [ "id"=> 638, "HSN"=> 6301, ], [ "id"=> 765, "HSN"=> 7317, ], [ "id"=> 6626, "HSN"=> 47071000, ], [ "id"=> 6727, "HSN"=> 48081000, ], [ "id"=> 6728, "HSN"=> 48084010, ] ]; usort($data, function($arr1, $arr2) { return strcmp($arr1['HSN'], $arr2['HSN']); }); dd($data);
关于usort和strcmp的解释也许你必须阅读文档 以便更深入地了解它。