對二維數組中的內容進行排列組合


 

最近在做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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM