PHP笛卡尔积的一种实现方式
2023-9-6 19:53:52 Author: www.yanglong.pro(查看原文) 阅读量:13 收藏

 $arr = [
            ['黑色', '红色', '白色', '黑色', '紫色'],
            ['36码', '37码', '38码', '39码', '40码'],
            ['男款', '女款'],
            ['运动', '休闲', '商务'],
        ];
        $count = count($arr);
        $result = $arr[0];
        $x = 0;
        while (true) {
            $x++;
            if ($x >= $count) break;
            $temp = [];
            foreach ($result as $item) {
                foreach ($arr[$x] as $item2) {
                    $temp[] = "$item-$item2";
                }
            }
            $result = $temp;
        }
        // 打印
        foreach ($result as $item) {
            echo "$item\n";
        }

文章来源: https://www.yanglong.pro/php%e7%ac%9b%e5%8d%a1%e5%b0%94%e7%a7%af%e7%9a%84%e4%b8%80%e7%a7%8d%e5%ae%9e%e7%8e%b0%e6%96%b9%e5%bc%8f/
如有侵权请联系:admin#unsafe.sh