PHP排列组合计算方法(如:$ource=[‘1L‘,‘5W-40‘,‘到店安装‘]的排列组合一共有6种实现方法如下)

原始数据$ource=['1L','5W-40','到店安装'];

想要实现的结果:

方法如下 

public function getCombineArrayByLength($source)
    {
        $result = [];
        sort($source); //保证初始数组是有序的
        $last = count($source) - 1; //$source尾部元素下标
        $x = $last;
        $count = 1; //组合个数统计
        array_push($result, implode(',', $source));
        // echo implode(',', $source), "<br>"; //输出第一种组合
        while (true) {
            $y = $x--; //相邻的两个元素
            if ($source[$x] < $source[$y]) { //如果前一个元素的值小于后一个元素的值
                $z = $last;
                while ($source[$x] > $source[$z]) { //从尾部开始,找到第一个大于 $x 元素的值
                    $z--;
                }
                /* 交换 $x 和 $z 元素的值 */
                list($source[$x], $source[$z]) = array($source[$z], $source[$x]);
                /* 将 $y 之后的元素全部逆向排列 */
                for ($i = $last; $i > $y; $i--, $y++) {
                    list($source[$i], $source[$y]) = array($source[$y], $source[$i]);
                }
                array_push($result, implode(',', $source));
                // echo implode(',', $source), "<br>"; //输出组合
                $x = $last;
                $count++;
            }
            if ($x == 0) { //全部组合完毕
                break;
            }
        }
        return $result;
    }

二维数组排列组合,在这个场景中,其实就是把多个属性的多个元素进行不重复的组合:,如:

使用计算机的解题思路,遍历所有属性,每次将一个属性的所有元素放入最终结果集中,并将结果作为模板,代入下一个属性计算中,代码如下 

public function getTwoCombineArray($options){
        $result=[];
        foreach ($options as $k => $v) {
            if(count($result)>0){
                $clone= $result;//将第一列作为模板
                $result=[];//置空当前列表,因为只有第一列的数据,组合是不完整的
                //遍历当前列,追加到模板中,使模板中的组合变得完整
                foreach ($v as $k1 => $v1) {
                    $temp=$clone;
                    foreach ($temp as $index => $value) {
                        $value[$k]=$v1;
                        $temp[$index]=$value;
                    }
                  //将完整的组合拼回到模板中
                    $result=array_merge($result,$temp);
                }
            }else{
               //先计算出第一列
                foreach ($v as $item) {
                    $result[][$k]= $item;
                }
            }
        }
        return $result;
    }

ource Type VolumeSnapshot by Name new-snapshot-demo: snapshot new-snapshot-demo not bound I0714 08:37:27.829848 1 event.go:282] Event(v1.ObjectReference{Kind:"PersistentVolumeClaim", Namespace:"default", Name:"snapshot-demo-restore", UID:"8dd75b4b-9a16-4940-b078-7088a672a649", APIVersion:"v1", ResourceVersion:"2855605", FieldPath:""}): type: 'Warning' reason: 'ProvisioningFailed' failed to provision volume with StorageClass "evs-sc": error getting handle for DataSource Type VolumeSnapshot by Name new-snapshot-demo: snapshot new-snapshot-demo not bound I0714 08:38:13.719937 1 controller.go:1279] provision "default/snapshot-demo-restore" class "evs-sc": started I0714 08:38:13.720202 1 event.go:282] Event(v1.ObjectReference{Kind:"PersistentVolumeClaim", Namespace:"default", Name:"snapshot-demo-restore", UID:"8dd75b4b-9a16-4940-b078-7088a672a649", APIVersion:"v1", ResourceVersion:"2868531", FieldPath:""}): type: 'Normal' reason: 'Provisioning' External provisioner is provisioning volume for claim "default/snapshot-demo-restore" W0714 08:38:13.723506 1 controller.go:933] Retrying syncing claim "8dd75b4b-9a16-4940-b078-7088a672a649", failure 31 E0714 08:38:13.723534 1 controller.go:956] error syncing claim "8dd75b4b-9a16-4940-b078-7088a672a649": failed to provision volume with StorageClass "evs-sc": error getting handle for DataSource Type VolumeSnapshot by Name new-snapshot-demo: snapshot new-snapshot-demo not bound I0714 08:38:13.723562 1 event.go:282] Event(v1.ObjectReference{Kind:"PersistentVolumeClaim", Namespace:"default", Name:"snapshot-demo-restore", UID:"8dd75b4b-9a16-4940-b078-7088a672a649", APIVersion:"v1", ResourceVersion:"2868531", FieldPath:""}): type: 'Warning' reason: 'ProvisioningFailed' failed to provision volume with StorageClass "evs-sc": error getting handle for DataSource Type VolumeSnapshot by Name new-snapshot-demo: snapshot new-snapshot-demo not bound
07-15
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值