近期在開發一個電商系統,做到了商品模組。雖然增刪改查在所難免,但是商品這塊更多的是js的調用等效果。還有更重要的就是商品sku的大量生成與組合了。
不同的系統有不同的需求,以下的需求就是要添加各種規格後批量生成sku的。 【推薦學習:PHP影片教學】
效果圖
頁面程式碼這裡就不說了,sku的生成方式是根據所有規格進行組合,也就是數學上的笛卡兒積,我用php寫了兩種方式,記錄一下。
實際開發中,你只要產生上面的二維數組就可以組合了,效果如下:
Array ( [0] => 白色-64G-移动-国行 [1] => 白色-64G-移动-港版 [2] => 白色-64G-电信-国行 [3] => 白色-64G-电信-港版 [4] => 白色-64G-联通-国行 [5] => 白色-64G-联通-港版 [6] => 白色-128G-移动-国行 [7] => 白色-128G-移动-港版 [8] => 白色-128G-电信-国行 [9] => 白色-128G-电信-港版 [10] => 白色-128G-联通-国行 [11] => 白色-128G-联通-港版 [12] => 银色-64G-移动-国行 [13] => 银色-64G-移动-港版 [14] => 银色-64G-电信-国行 [15] => 银色-64G-电信-港版 [16] => 银色-64G-联通-国行 [17] => 银色-64G-联通-港版 [18] => 银色-128G-移动-国行 [19] => 银色-128G-移动-港版 [20] => 银色-128G-电信-国行 [21] => 银色-128G-电信-港版 [22] => 银色-128G-联通-国行 [23] => 银色-128G-联通-港版 [24] => 玫瑰金-64G-移动-国行 [25] => 玫瑰金-64G-移动-港版 [26] => 玫瑰金-64G-电信-国行 [27] => 玫瑰金-64G-电信-港版 [28] => 玫瑰金-64G-联通-国行 [29] => 玫瑰金-64G-联通-港版 [30] => 玫瑰金-128G-移动-国行 [31] => 玫瑰金-128G-移动-港版 [32] => 玫瑰金-128G-电信-国行 [33] => 玫瑰金-128G-电信-港版 [34] => 玫瑰金-128G-联通-国行 [35] => 玫瑰金-128G-联通-港版 )
輸出列印結果
Array ( [0] => Array ( [0] => 白色 [1] => 64G [2] => 移动 [3] => 国行 ) [1] => Array ( [0] => 白色 [1] => 64G [2] => 移动 [3] => 港版 ) [2] => Array ( [0] => 白色 [1] => 64G [2] => 电信 [3] => 国行 ) [3] => Array ( [0] => 白色 [1] => 64G [2] => 电信 [3] => 港版 ) [4] => Array ( [0] => 白色 [1] => 64G [2] => 联通 [3] => 国行 ) [5] => Array ( [0] => 白色 [1] => 64G [2] => 联通 [3] => 港版 ) [6] => Array ( [0] => 白色 [1] => 128G [2] => 移动 [3] => 国行 ) [7] => Array ( [0] => 白色 [1] => 128G [2] => 移动 [3] => 港版 ) [8] => Array ( [0] => 白色 [1] => 128G [2] => 电信 [3] => 国行 ) [9] => Array ( [0] => 白色 [1] => 128G [2] => 电信 [3] => 港版 ) [10] => Array ( [0] => 白色 [1] => 128G [2] => 联通 [3] => 国行 ) [11] => Array ( [0] => 白色 [1] => 128G [2] => 联通 [3] => 港版 ) [12] => Array ( [0] => 银色 [1] => 64G [2] => 移动 [3] => 国行 ) [13] => Array ( [0] => 银色 [1] => 64G [2] => 移动 [3] => 港版 ) [14] => Array ( [0] => 银色 [1] => 64G [2] => 电信 [3] => 国行 ) [15] => Array ( [0] => 银色 [1] => 64G [2] => 电信 [3] => 港版 ) [16] => Array ( [0] => 银色 [1] => 64G [2] => 联通 [3] => 国行 ) [17] => Array ( [0] => 银色 [1] => 64G [2] => 联通 [3] => 港版 ) [18] => Array ( [0] => 银色 [1] => 128G [2] => 移动 [3] => 国行 ) [19] => Array ( [0] => 银色 [1] => 128G [2] => 移动 [3] => 港版 ) [20] => Array ( [0] => 银色 [1] => 128G [2] => 电信 [3] => 国行 ) [21] => Array ( [0] => 银色 [1] => 128G [2] => 电信 [3] => 港版 ) [22] => Array ( [0] => 银色 [1] => 128G [2] => 联通 [3] => 国行 ) [23] => Array ( [0] => 银色 [1] => 128G [2] => 联通 [3] => 港版 ) [24] => Array ( [0] => 玫瑰金 [1] => 64G [2] => 移动 [3] => 国行 ) [25] => Array ( [0] => 玫瑰金 [1] => 64G [2] => 移动 [3] => 港版 ) [26] => Array ( [0] => 玫瑰金 [1] => 64G [2] => 电信 [3] => 国行 ) [27] => Array ( [0] => 玫瑰金 [1] => 64G [2] => 电信 [3] => 港版 ) [28] => Array ( [0] => 玫瑰金 [1] => 64G [2] => 联通 [3] => 国行 ) [29] => Array ( [0] => 玫瑰金 [1] => 64G [2] => 联通 [3] => 港版 ) [30] => Array ( [0] => 玫瑰金 [1] => 128G [2] => 移动 [3] => 国行 ) [31] => Array ( [0] => 玫瑰金 [1] => 128G [2] => 移动 [3] => 港版 ) [32] => Array ( [0] => 玫瑰金 [1] => 128G [2] => 电信 [3] => 国行 ) [33] => Array ( [0] => 玫瑰金 [1] => 128G [2] => 电信 [3] => 港版 ) [34] => Array ( [0] => 玫瑰金 [1] => 128G [2] => 联通 [3] => 国行 ) [35] => Array ( [0] => 玫瑰金 [1] => 128G [2] => 联通 [3] => 港版 ) )
本文系轉載,原文位址:https://juejin.cn/post/7170889538343501838
以上是php開發商品批量生成sku組合(兩種方式)的詳細內容。更多資訊請關注PHP中文網其他相關文章!