PHP implements Browse records and groups by date

Source: Internet
Author: User
This article mainly introduces the content is about PHP implementation of browsing records and grouped by date, has a certain reference value, now share to everyone, the need for friends can refer to

The existing test data is as follows, which requires the implementation of the effect as in:





array (4) {[0] = = Array (6) {["visit_id"] = = Int (127) ["goods_id"] + int (+) ["visittime"] + int (1494399935) ["goods_name"] + = string (+) "OPPO r9s all netcom 4g+64g dual sim phone rose gold" ["Shop_price"] = = String (6) "500.00"} [1] = = Array (6) {["visit_id"] = + int (124) ["goods_id"] + int (all) ["visittime"] = > int (1494399921) ["Goods_name"] + string ("siemens/") "Siemens Ka92nv09ti dual-door home-to-door refrigerator inverter flagship" ["shop_price"] = String (7) "4000.00"} [2] = = Array (6) {["visit_id"] = + int (123) ["goods_id"] + int (+) ["visittime"] = int (1494399903) ["Goods_name"] + string (85) "Love him whitening Gold Edition Aptamil infant Formula 3 paragraph (12-36 months apply) 900g (European imports)" ["shop_price"] = String (6) "329.00"} [3] = = Array (6) {["visit_id"] = + int (+) ["goods_id"] + int (+) ["visittime"] = I NT (1494224263) ["Goods_name"] + string (21) "Happy Valley" ["Shop_price"] + string (5) "50.00"}} 



Before I realized that this code does not know whether to O (n) or a higher degree of complexity to achieve, think about it, in fact, the bottom of the PHP array is a hash implementation, how to simply use this feature to reduce the complexity of the O (1)?

Show Me the Code:






/* Browse records grouped by date */function groupvisit ($visit) {  $curyear = date (' Y ');  $visit _list = [];  foreach ($visit as $v) {   if ($curyear = = Date (' Y ', $v [' Visittime '])) {    $date = date (' m ' D Day ', $v [' visittime ']);   } else {    $date = date (' Y year m D Day ', $v [' visittime ']);   }   $visit _list[$date] [] = $v;  }  return $visit _list; }


Description: The entry $visit is the first test data, the function also made some optimization of the user experience, for example, your browsing history is always showing the year, is not feeling a bit redundant, so if this year, the year will be hidden, the other years will still show up. Otherwise the code will be more concise.

Okay, the above test data passes through the function, then prints the return value:






2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34


Array (2) {["May 10"] = = Array (3) {[0] = = Array (6) {["visit_id"] = + int (127) ["goods_id"] + int (16) ["visittime"] = Int (1494399935) ["Goods_name"] + string (+) "OPPO r9s all netcom 4g+64g dual sim phone rose gold" ["Shop_price"] =&G T String (6) "500.00"} [1] = = Array (6) {["visit_id"] = + int (124) ["goods_id"] + int (all) ["Visittime"] =&gt ; int (1494399921) ["Goods_name"] + string ("7") "siemens/Siemens Ka92nv09ti Double-door open-door refrigerator inverter flagship model" ["Shop_price"] and "["] = string ) "4000.00"} [2] = = Array (6) {["visit_id"] = + int (123) ["goods_id"] + int (+) ["visittime"] = + int (1 494399903) ["Goods_name"] + string (85) "Love him whitening Gold Edition Aptamil infant Formula 3 paragraph (12-36 months apply) 900g (European imports)" ["Shop_price"] and string (6 ) "329.00"}} ["May 08"] = = Array (1) {[0] = = Array (6) {["visit_id"] + int (+) ["goods_id"] = = Int ( ["Visittime"] + int (1494224263) ["Goods_name"] and string (21) "Happy Valley" ["Shop_price"] and string (5) "50. 00 "}}}


That's right, that's exactly what I want.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.