Home > Article > Backend Development > PHP implements browsing records and groups them by date
The main content of this article is about PHP's implementation of browsing records and grouping by date. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
The existing test data is as follows , it is required to achieve the effect as shown below:
array(4) { [0] => array(6) { ["visit_id"] => int(127) ["goods_id"] => int(16) ["visittime"] => int(1494399935) ["goods_name"] => string(53) "OPPO R9s 全网通4G+64G 双卡双待手机 玫瑰金" ["shop_price"] => string(6) "500.00" } [1] => array(6) { ["visit_id"] => int(124) ["goods_id"] => int(13) ["visittime"] => int(1494399921) ["goods_name"] => string(76) "SIEMENS/西门子 KA92NV09TI双开门家用对开门电冰箱变频旗舰款" ["shop_price"] => string(7) "4000.00" } [2] => array(6) { ["visit_id"] => int(123) ["goods_id"] => int(27) ["visittime"] => int(1494399903) ["goods_name"] => string(85) "爱他美白金版Aptamil 幼儿配方奶粉3段(12-36个月适用)900g(欧洲进口)" ["shop_price"] => string(6) "329.00" } [3] => array(6) { ["visit_id"] => int(120) ["goods_id"] => int(26) ["visittime"] => int(1494224263) ["goods_name"] => string(21) "欢乐谷免票一张" ["shop_price"] => string(5) "50.00" } } |
##Before I implemented it, I didn’t know whether this code would require O(n) or higher complexity. If I think about it carefully, In fact, the bottom layer of PHP's array is implemented by hashing. How to simply use this feature to reduce the complexity to O(1)?
show me the code:
#/* 浏览记录按日期分组 */ function groupVisit($visit) { $curyear = date('Y'); $visit_list = []; foreach ($visit as $v) { if ($curyear == date('Y', $v['visittime'])) { $date = date('m月d日', $v['visittime']); } else { $date = date('Y年m月d日', $v['visittime']); } $visit_list[$date][] = $v; } return $visit_list; } |
2 | ##
|
Related recommendations:
How to reverse Chinese strings in PHP to avoid garbled charactersThe above is the detailed content of PHP implements browsing records and groups them by date. For more information, please follow other related articles on the PHP Chinese website!