PHP获取所有组合商品属性组成的数组

PHP获取所有组合商品属性组成的数组

组合商品,获取每一种组合的结果。代码如下

private $models=[['RED','BLUE','GREEN'],['低配','中配','高配'],['进口','国产']];
    private $index=0;

    /**
     * 组合
     */
    public function combineAttributes(){
        $m_len=count($this->models);
        if($m_len>=2){
            $result=$this->recurse($this->models[$this->index],$this->models[++$this->index],$m_len);
        }else{
            $result=$this->models[0];
        }
        print_r($result);
    }

    /**
     * 递归拼接属性
     * @param $array1
     * @param $array2
     * @param $m_len
     * @return array
     */
    public function recurse($array1,$array2,$m_len){
        $res=[];
        foreach ($array1 as $a1){
            foreach ($array2 as $a2){
                array_push($res,$a1.'-'.$a2);
            }
        }
        $this->index++;
        if($this->index<=$m_len-1){
            return $this->recurse($res,$this->models[$this->index],$m_len);
        }else{
            return $res;
        }
    }

打印结果如下

Array
(
    [0] => RED-低配-进口
    [1] => RED-低配-国产
    [2] => RED-中配-进口
    [3] => RED-中配-国产
    [4] => RED-高配-进口
    [5] => RED-高配-国产
    [6] => BLUE-低配-进口
    [7] => BLUE-低配-国产
    [8] => BLUE-中配-进口
    [9] => BLUE-中配-国产
    [10] => BLUE-高配-进口
    [11] => BLUE-高配-国产
    [12] => GREEN-低配-进口
    [13] => GREEN-低配-国产
    [14] => GREEN-中配-进口
    [15] => GREEN-中配-国产
    [16] => GREEN-高配-进口
    [17] => GREEN-高配-国产
)

此代码参考其他博友的博客:https://shenchuochuo.iteye.com/blog/2306646
对此表示感谢!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值