• php多个数组组合算法 火车头免登录发布接口代码备忘


    火车头发布产品的时候,有颜色、尺码、性别等等产品属性,需要进行不重复的组合,变成不重复的数组

    <?php
    function comb($a){
        $a = array_filter($a);
        $out = array();
        if (count($a) == 1) {
            $r = array_shift($a);
            foreach ($r as $v) $out[] = array($v);
            return $out;
        }
        foreach ($a as $k => $v){
            $b = $a;
            unset($b[$k]);
            $r = comb($b);
            foreach ($v as $v1){
                foreach ($r As $v2)
                $out[] = array_merge(array($v1), $v2);
            }
            break;
        }
        return $out;
    }
    
    
    $array1 = array(array('Red', 'Black', 'Yellow'),array('Boy','Girl'),array('S', 'M', 'L'));
    $result = comb($array1);
    print_r($result);
    ?>

    返回的结果如下:

    Array
    (
        [0] => Array
            (
                [0] => Red
                [1] => Boy
                [2] => S
            )
    
        [1] => Array
            (
                [0] => Red
                [1] => Boy
                [2] => M
            )
    
        [2] => Array
            (
                [0] => Red
                [1] => Boy
                [2] => L
            )
    
        [3] => Array
            (
                [0] => Red
                [1] => Girl
                [2] => S
            )
    
        [4] => Array
            (
                [0] => Red
                [1] => Girl
                [2] => M
            )
    
        [5] => Array
            (
                [0] => Red
                [1] => Girl
                [2] => L
            )
    
        [6] => Array
            (
                [0] => Black
                [1] => Boy
                [2] => S
            )
    
        [7] => Array
            (
                [0] => Black
                [1] => Boy
                [2] => M
            )
    
        [8] => Array
            (
                [0] => Black
                [1] => Boy
                [2] => L
            )
    
        [9] => Array
            (
                [0] => Black
                [1] => Girl
                [2] => S
            )
    
        [10] => Array
            (
                [0] => Black
                [1] => Girl
                [2] => M
            )
    
        [11] => Array
            (
                [0] => Black
                [1] => Girl
                [2] => L
            )
    
        [12] => Array
            (
                [0] => Yellow
                [1] => Boy
                [2] => S
            )
    
        [13] => Array
            (
                [0] => Yellow
                [1] => Boy
                [2] => M
            )
    
        [14] => Array
            (
                [0] => Yellow
                [1] => Boy
                [2] => L
            )
    
        [15] => Array
            (
                [0] => Yellow
                [1] => Girl
                [2] => S
            )
    
        [16] => Array
            (
                [0] => Yellow
                [1] => Girl
                [2] => M
            )
    
        [17] => Array
            (
                [0] => Yellow
                [1] => Girl
                [2] => L
            )
    
    )
    转载请注明出处!小鱼阁工作室 -专注zencart建站,织梦企业建站,ecshop商城,二次开发,产品采集,模板修改!技术QQ 631992791
  • 相关阅读:
    Linux 小知识翻译
    Linux 小知识翻译
    Linux 小知识翻译
    Linux 小知识翻译
    Linux 小知识翻译
    Linux 小知识翻译
    Linux 小知识翻译
    PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***
    Internet Download Manager 快速下载插件,破解版
    微积分基本公式(牛顿——莱布尼茨公式)的几何解释
  • 原文地址:https://www.cnblogs.com/afish/p/3997139.html
Copyright © 2020-2023  润新知