对二维数组中的内容进行排列组合

最近在做ecshop的商品库存模块,分别给一款商品的多个属性组合设置库存,如下图:

 

 一款手机有不同颜色,屏幕尺寸,系统和电量,都要设置不同的库存,如果都要手动选择属性组合,则会耗费很多不必要的时间。假如打开页面时就已经设置好属性排列组合那就最好不过,因此想了整天,写了如下函数:

    /*
    2 Author:GaZeon
    3 Date:2016-6-20
    4 Function:getArrSet
    5 Param:$arrs 二维数组
    6 getArrSet(array(array(),...))
    7 数组不重复排列集合
    8 */
    function getArrSet($arrs, $_current_index = -1)
    {
        //总数组
        static $_total_arr;
        //总数组下标计数
        static $_total_arr_index;
        //输入的数组长度
        static $_total_count;
        //临时拼凑数组
        static $_temp_arr;

        //进入输入数组的第一层,清空静态数组,并初始化输入数组长度
        if ($_current_index < 0) {
            $_total_arr = array();
            $_total_arr_index = 0;
            $_temp_arr = array();
            $_total_count = count($arrs) - 1;
            $this->getArrSet($arrs, 0);
        } else {
            //循环第$_current_index层数组
            foreach ($arrs[$_current_index] as $rkey=>$v) {
                if($rkey==0){
                    continue;
                }
                //如果当前的循环的数组少于输入数组长度
                if ($_current_index < $_total_count) {
                    //将当前数组循环出的值放入临时数组
                    $_temp_arr[$_current_index] = $v;
                    //继续循环下一个数组
                    $this->getArrSet($arrs, $_current_index + 1);

                } //如果当前的循环的数组等于输入数组长度(这个数组就是最后的数组)
                else if ($_current_index == $_total_count) //将当前数组循环出的值放入临时数组
                {
                    $_temp_arr[$_current_index] = $v;
                    //将临时数组加入总数组
                    $_total_arr[$_total_arr_index] = $_temp_arr;
                    //总数组下标计数+1
                    $_total_arr_index++;
                }
            }
        }
        return $_total_arr;//返回值为一个多维数组,将每一个子数组的值连接起来就是组合后的值
    }

$arr = array(
array('a', 'b', 'c'),
array('A', 'B', 'C'),
array('I','II','III')
);
$result = $this->getArrSet($arr);
var_dump($result);
 

如果需要组合内容的来源坐标使用下面这个方法

/*
2 Author:GaZeon
3 Date:2016-6-20
4 Function:getArrSet
5 Param:$arrs 二维数组
6 getArrSet(array(array(),...))
7 数组不重复排列集合
8 */
function getArrSet($arrs, $_current_index = -1)
{
    //总数组
    static $_total_arr;
    //总数组下标计数
    static $_total_arr_index;
    //输入的数组长度
    static $_total_count;
    //临时拼凑数组
    static $_temp_arr;

    //进入输入数组的第一层,清空静态数组,并初始化输入数组长度
    if ($_current_index < 0) {
        $_total_arr = array();
        $_total_arr_index = 0;
        $_temp_arr = array();
        $_total_count = count($arrs) - 1;
        $this->getArrSet($arrs, 0);
    } else {
        //循环第$_current_index层数组
        foreach ($arrs[$_current_index] as $rkey=>$v) {
            //如果当前的循环的数组少于输入数组长度
            if ($_current_index < $_total_count) {
                //将当前数组循环出的值放入临时数组
                $_temp_arr[$_current_index] = $v.':'.$_current_index.','.$rkey;
                //继续循环下一个数组
                $this->getArrSet($arrs, $_current_index + 1);

            } //如果当前的循环的数组等于输入数组长度(这个数组就是最后的数组)
            else if ($_current_index == $_total_count) //将当前数组循环出的值放入临时数组
            {
                $_temp_arr[$_current_index] = $v.':'.$_current_index.','.$rkey;
                //将临时数组加入总数组
                $_total_arr[$_total_arr_index] = $_temp_arr;
                //总数组下标计数+1
                $_total_arr_index++;
            }
        }
    }
    return $_total_arr;//返回值为一个数组,数组的值 冒号之前为组合内容,冒号之后为组合内容在原数组中的坐标
}
$arr = array(
array('a', 'b', 'c'),
array('A', 'B', 'C'),
array('I','II','III')
);
$result = $this->getArrSet($arr);
var_dump($result);

猜你喜欢

转载自www.cnblogs.com/beiman/p/11542184.html