PHP method to implement browsing records and grouping by date

墨辰丷
Release: 2023-03-27 13:34:01
Original
1201 people have browsed it

This article mainly introduces the example code of PHP to implement browsing records and group by date. It is very good and has reference value. Friends in need can refer to it.

The existing test data is as follows, and the requirements are as follows The effect in the picture:

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 wondered whether this code would be O(n) or not. Higher complexity can be achieved. If you think about it carefully, the bottom layer of PHP's array is actually 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

Instructions: The input parameter $visit is the test data at the beginning above, and the user experience is also included in the function Some optimizations have been made. For example, this year's year is always displayed in your browsing history. Doesn't it feel 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 correct It's the effect I want.

Related recommendations:

PHP implements browsing records and groups by date

Ask a mysql , group by Date grouping Query related issues

DateTime processing tool example detailed explanation in PHP

The above is the detailed content of PHP method to implement browsing records and grouping 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!