请帮助我解决排序问题" Row"按降序排列"列"按JSON数据下面的升序排列。
$print = '{"Table":[
{"Column":2,"Length":1,"Number":"A4","Row":12},
{"Column":2,"Length":1,"Number":"A3","Row":11},
{"Column":2,"Length":1,"Number":"A2","Row":9},
{"Column":2,"Length":1,"Number":"A1","Row":8},
{"Column":3,"Length":1,"Number":"B4","Row":12},
{"Column":3,"Length":1,"Number":"B3","Row":11},
{"Column":3,"Length":1,"Number":"B2","Row":9},
{"Column":3,"Length":1,"Number":"B1","Row":8},
{"Column":4,"Length":1,"Number":"C4","Row":12},
{"Column":4,"Length":1,"Number":"C3","Row":11},
{"Column":4,"Length":1,"Number":"C2","Row":9},
{"Column":4,"Length":1,"Number":"C1","Row":8},
{"Column":5,"Length":1,"Number":"D4","Row":12},
{"Column":5,"Length":1,"Number":"D3","Row":11},
{"Column":5,"Length":1,"Number":"D2","Row":9},
{"Column":5,"Length":1,"Number":"D1","Row":8},
{"Column":6,"Length":1,"Number":"E4","Row":12},
{"Column":6,"Length":1,"Number":"E3","Row":11},
{"Column":6,"Length":1,"Number":"E2","Row":9},
{"Column":6,"Length":1,"Number":"E1","Row":8},
{"Column":7,"Length":1,"Number":"F4","Row":12},
{"Column":7,"Length":1,"Number":"F3","Row":11},
{"Column":7"Length":1,"Number":"F2","Row":9},
{"Column":7,"Length":1,"Number":"F1","Row":8},
{"Column":8,"Length":1,"Number":"G4","Row":12},
{"Column":8,"Length":1,"Number":"G3","Row":11},
{"Column":8,"Length":1,"Number":"G2","Row":9},
{"Column":8,"Length":1,"Number":"G1","Row":8},
{"Column":9,"Length":1,"Number":"H4","Row":12},
{"Column":9,"Length":1,"Number":"H3","Row":11},
{"Column":9,"Length":1,"Number":"H2","Row":9},
{"Column":9,"Length":1,"Number":"H1","Row":8},
{"Column":10,"Length":1,"Number":"I4","Row":12},
{"Column":10,"Length":1,"Number":"I3","Row":11},
{"Column":10,"Length":1,"Number":"I2","Row":9},
{"Column":10,"Length":1,"Number":"I1","Row":8},
{"Column":11,"Length":1,"Number":"J4","Row":12},
{"Column":11,"Length":1,"Number":"J3","Row":11},
{"Column":11,"Length":1,"Number":"J2","Row":9},
{"Column":11,"Length":1,"Number":"J1","Row":8},
{"Column":12,"Length":1,"Number":"K4","Row":12},
{"Column":12,"Length":1,"Number":"K3","Row":11},
{"Column":12,"Length":1,"Number":"K2","Row":9},
{"Column":12,"Length":1,"Number":"K1","Row":8},
{"Column":13,"Length":1,"Number":"S4","Row":12},
{"Column":13,"Length":1,"Number":"S3","Row":11},
{"Column":13,"Length":1,"Number":"S5","Row":10},
{"Column":13,"Length":1,"Number":"S2","Row":9},
{"Column":13,"Length":1,"Number":"S1","Row":8}],"ResponseStatus":200,"Message":null}';
我想按" Row"应该按降序打印,"列"相应的"行组"应按升序打印。我想要休息如下。
$print = '{"Table":[
{"Column":2,"Length":1,"Number":"A4","Row":12},
{"Column":3,"Length":1,"Number":"B4","Row":12},
{"Column":4,"Length":1,"Number":"C4","Row":12},
{"Column":5,"Length":1,"Number":"D4","Row":12},
{"Column":6,"Length":1,"Number":"E4","Row":12},
{"Column":7,"Length":1,"Number":"F4","Row":12},
{"Column":8,"Length":1,"Number":"G4","Row":12},
{"Column":9,"Length":1,"Number":"H4","Row":12},
{"Column":10,"Length":1,"Number":"I4","Row":12},
{"Column":11,"Length":1,"Number":"J4","Row":12},
{"Column":12,"Length":1,"Number":"K4","Row":12},
{"Column":13,"Length":1,"Number":"S4","Row":12},
{"Column":2,"Length":1,"Number":"A3","Row":11},
{"Column":3,"Length":1,"Number":"B3","Row":11},
{"Column":4,"Length":1,"Number":"C3","Row":11},
{"Column":5,"Length":1,"Number":"D3","Row":11},
{"Column":6,"Length":1,"Number":"E3","Row":11},
{"Column":7,"Length":1,"Number":"F3","Row":11},
{"Column":8,"Length":1,"Number":"G3","Row":11},
{"Column":9,"Length":1,"Number":"H3","Row":11},
{"Column":10,"Length":1,"Number":"I3","Row":11},
{"Column":11,"Length":1,"Number":"J3","Row":11},
{"Column":12,"Length":1,"Number":"K3","Row":11},
{"Column":13,"Length":1,"Number":"S3","Row":11},
{"Column":13,"Length":1,"Number":"S5","Row":10},
{"Column":2,"Length":1,"Number":"A2","Row":9},
{"Column":3,"Length":1,"Number":"B2","Row":9},
{"Column":4,"Length":1,"Number":"C2","Row":9},
{"Column":5,"Length":1,"Number":"D2","Row":9},
{"Column":6,"Length":1,"Number":"E2","Row":9},
{"Column":7"Length":1,"Number":"F2","Row":9},
{"Column":8,"Length":1,"Number":"G2","Row":9},
{"Column":9,"Length":1,"Number":"H2","Row":9},
{"Column":10,"Length":1,"Number":"I2","Row":9},
{"Column":11,"Length":1,"Number":"J2","Row":9},
{"Column":12,"Length":1,"Number":"K2","Row":9},
{"Column":13,"Length":1,"Number":"S2","Row":9},
{"Column":2,"Length":1,"Number":"A1","Row":8},
{"Column":3,"Length":1,"Number":"B1","Row":8},
{"Column":4,"Length":1,"Number":"C1","Row":8},
{"Column":5,"Length":1,"Number":"D1","Row":8},
{"Column":6,"Length":1,"Number":"E1","Row":8},
{"Column":7,"Length":1,"Number":"F1","Row":8},
{"Column":8,"Length":1,"Number":"G1","Row":8},
{"Column":9,"Length":1,"Number":"H1","Row":8},
{"Column":10,"Length":1,"Number":"I1","Row":8},
{"Column":11,"Length":1,"Number":"J1","Row":8},
{"Column":12,"Length":1,"Number":"K1","Row":8},
{"Column":13,"Length":1,"Number":"S1","Row":8}],"ResponseStatus":200,"Message":null}';
请关注" Row"这是打印的降序和"列"按升序排列。
我使用了以下代码,但这有助于只缩短Row但是" Columns"没有正确排序。
$json = json_decode($print, true);
usort($json['Table'], function($a, $b) { return $a['Row'] < $b['Row']; } );
$print = json_encode($json);
请帮我解决这个问题。提前谢谢。
答案 0 :(得分:0)
如果它的行是降序,那么它应该是b - a
,而对于列,它应该是a -b
:
usort($json['Table'], function($a, $b) {
$diff = $b['Row'] - $a['Row'];
if($diff) return $diff;
return $a['Column'] - $b['Column'];
});