PHP implements browsing records and groups them by date

不言
Release: 2023-03-24 06:02:01
Original
1350 people have browsed it

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"
 } 
}
Copy after login



##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; 
}
Copy after login



Explanation: The input parameter $visit is the test data at the beginning above. The function has also made some optimizations to the user experience. For example, your browsing history always displays This year's year seems a bit redundant, so if it is this year, the year will be hidden, and other years will still be displayed. Otherwise the code will be cleaner.

After the above test data passes through this function, the return value is printed out:





##
array(2) { 
 ["05月10日"] => array(3) { 
 [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"
 } 
 } 
 ["05月08日"] => array(1) { 
 [0] => array(6) { 
  ["visit_id"] => int(120) 
  ["goods_id"] => int(26) 
  ["visittime"] => int(1494224263) 
  ["goods_name"] => string(21) "欢乐谷免票一张"
  ["shop_price"] => string(5) "50.00"
 } 
 } 
}
Copy after login

Yes, this is exactly the effect I want.

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34



Related recommendations:

How to reverse Chinese strings in PHP to avoid garbled characters

The 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!