PHP implements browsing records and groups by date. php records the date group.

Source: Internet
Author: User

PHP implements browsing records and groups by date. php records the date group.

The existing test data is as follows, which requires the following results:

Array (4) {[0] => array (6) {["visit_id"] => int (127) ["goods_id"] => int (16) ["visittime"] => int (1494399935) ["goods_name"] => string (53) "OPPO R9s full Netcom 4G + 64G dual-card dual-Wait mobile phone rose gold" ["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/SIEMENS KA92NV09TI dual-door-to-door refrigerator inverter flagship model" ["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 platinum edition Infant Formula 3 (12-36 months applicable) 900g (imported from Europe) "[" shop_price "] => string (6)" 329.00 "} [3] => array (6) {["visit_id"] => int (120) ["goods_id"] => int (26) ["visittime"] => int (1494224263) ["goods_name"] => string (21) "Happy Valley free one" ["shop_price"] => string (5) "50.00 "}}

Before implementation, I think this code does not know whether O (n) is required or whether it is more complex to implement. I think about it. In fact, the underlying layer of the php array is implemented by hashing, how can we use this feature to reduce complexity to O (1?

Show me the code:

/* Browsing records are grouped by date */function groupVisit ($ visit) {$ curyear = date ('y'); $ visit_list = []; foreach ($ visit as $ v) {if ($ curyear = date ('y', $ v ['visittime']) {$ date = date ('m month dday ', $ v ['visittime']);} else {$ date = date ('y, m, dday', $ v ['visittime']);} $ visit_list [$ date] [] = $ v;} return $ visit_list ;}

Note: The input parameter $ visit is the test data from the beginning. The function also optimizes the user experience. For example, your browsing records always show the year of this year, is it a bit redundant, so if it is the current year, the year will be hidden, and other years will still be displayed. Otherwise, the code will be more concise.

After the above test data passes through the function, print the return value, which is:

Array (2) {["May 10"] => array (3) {[0] => array (6) {["visit_id"] => int (127) ["goods_id"] => int (16) ["visittime"] => int (1494399935) ["goods_name"] => string (53) "OPPO R9s full Netcom 4G + 64G dual-card dual-Wait mobile phone rose gold" ["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/SIEMENS KA92NV09TI dual-door-to-door refrigerator inverter flagship model" ["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 platinum edition Infant Formula 3 (12-36 months applicable) 900g (imported from Europe) "[" shop_price "] => string (6)" 329.00 "} [" May 08 "] => array (1) {[0] => array (6) {["visit_id"] => int (120) ["goods_id"] => int (26) ["visittime"] => int (1494224263) ["goods_name"] => string (21) "Happy Valley free one" ["shop_price"] => string (5) "50.00 "}}}

Yes, this is exactly what I want.

The above is a small Editor to introduce you to the PHP browser records and grouping by date, I hope to help you, if you have any questions, please leave a message, the small editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.