您好,登錄后才能下訂單哦!
這篇文章主要介紹PHP如何實(shí)現(xiàn)瀏覽記錄并按日期分組,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
現(xiàn)有測(cè)試數(shù)據(jù)如下,要求實(shí)現(xiàn)如下圖中的效果:
array(4) { [0] => array(6) { ["visit_id"] => int(127) ["goods_id"] => int(16) ["visittime"] => int(1494399935) ["goods_name"] => string(53) "OPPO R9s 全網(wǎng)通4G+64G 雙卡雙待手機(jī) 玫瑰金" ["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雙開門家用對(duì)開門電冰箱變頻旗艦款" ["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個(gè)月適用)900g(歐洲進(jìn)口)" ["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" } }
還沒實(shí)現(xiàn)之前我想這個(gè)代碼不知道要O(n)呢還是更高的復(fù)雜度才能實(shí)現(xiàn),細(xì)想一下,其實(shí)php的數(shù)組底層就是哈希實(shí)現(xiàn)的,如何簡(jiǎn)單利用這個(gè)特性使復(fù)雜度降到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; }
說明:入?yún)?$visit 就是上面一開始的測(cè)試數(shù)據(jù),函數(shù)中還對(duì)用戶體驗(yàn)做了一些優(yōu)化,比如,你的瀏覽記錄中總是顯示出今年的年份,是不是感覺有點(diǎn)冗余,所以如果是本年份的話,年份就要隱藏起來,其他年份還是照樣顯示出來。不然代碼會(huì)更加簡(jiǎn)潔。
好了上面的測(cè)試數(shù)據(jù)經(jīng)過該函數(shù)之后,再把返回值打印出來,有:
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 全網(wǎng)通4G+64G 雙卡雙待手機(jī) 玫瑰金" ["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雙開門家用對(duì)開門電冰箱變頻旗艦款" ["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個(gè)月適用)900g(歐洲進(jìn)口)" ["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" } } }
對(duì)的,這正是我想要的效果。
php是一個(gè)嵌套的縮寫名稱,是英文超級(jí)文本預(yù)處理語(yǔ)言,它的語(yǔ)法混合了C、Java、Perl以及php自創(chuàng)新的語(yǔ)法,主要用來做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因?yàn)閜hp是開源的,從而使得php經(jīng)久不衰。
以上是“PHP如何實(shí)現(xiàn)瀏覽記錄并按日期分組”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。