如何使用identicel键值对JSON数据进行分组?

时间:2015-02-27 19:00:32

标签: php json

任何人都可以教我如何根据数据中给出的键值对JSON数据进行分组。在这里,我举了一个例子。

我有以下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}],"ResponseStatus":200,"Message":null}';

我想根据下面给出的“行”值对这些数据进行分组。

$print = '{"Table":[
{"Column":2,"Length":1,"Number":"A4","Row":12}, 
{"Column":3,"Length":1,"Number":"B4","Row":12},
{"Column":2,"Length":1,"Number":"A3","Row":11},  
{"Column":3,"Length":1,"Number":"B3","Row":11},
{"Column":2,"Length":1,"Number":"A2","Row":9},
{"Column":3,"Length":1,"Number":"B2","Row":9},
{"Column":2,"Length":1,"Number":"A1","Row":8},  
{"Column":3,"Length":1,"Number":"B1","Row":8}],"ResponseStatus":200,"Message":null}';

我用谷歌搜索了好几个小时,但我找不到解决办法。我想用PHP分组。请有人帮我解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:1)

$json = json_decode($print, true);
usort($json['Table'], function($a, $b) { return $a['Row'] < $b['Row']; } );
$print = json_encode($json);