Recently doing ecshop commodity inventory module, respectively, to the plurality of combinations of attributes of a product inventory is provided, as shown below:
A cell phone has a different color, screen size, and power systems, should set a different stock, if have to manually select the combination of attributes, will be a lot of unnecessary time-consuming. If you already set when you open the page properties of permutations and combinations would be best, however, and therefore want the whole day, I wrote the following function:
/ * 2 the Author: GaZeon . 3 a Date: 2016-6-20 . 4 Function: getArrSet . 5 the Param: $ ARRS two-dimensional array . 6 getArrSet (Array (Array (), ...)) . 7 array does not duplicate set of permutations . 8 * / function getArrSet (ARRS $, $ _current_index = - . 1 ) { // total number of groups static $ _total_arr; // total number of groups indexed count static $ _total_arr_index; // array length input static $ _total_count; // makeshift array static $ _temp_arr; // into the input layer of the first array, a static array emptied, and the array length initialization input 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 the current cycle is less than the array length of an array of the input IF (_current_index $ < $ _total_count) { // the current value of the loop out of the array into a temporary array $ _temp_arr [CURRENT_INDEX of $ _] = $ V; // continue the cycle an array $ the this -> getArrSet (ARRS $, $ + _current_index . 1 ); } // if the current cycle is equal to the array length of the input array (the array is the final array) the else IF (== $ $ _current_index _total_count) // an array of loop current value into a temporary array { $ _temp_arr [CURRENT_INDEX of $ _] = $ V; // the total number of groups added temporary array _total_arr $ [$ _ total_arr_index] = $ _temp_arr; // total number of groups indexed count + 1'd $ _total_arr_index ++ ; } } } return $ _total_arr; after // the return value of a multidimensional array, the value of each sub-array are connected together is a combination of value }
$arr = array(
array('a', 'b', 'c'),
array('A', 'B', 'C'),
array('I','II','III')
);
$result = $this->getArrSet($arr);
var_dump ($ result);
If the source coordinates need to combine content using the following method
/ * 2 the Author: GaZeon . 3 a Date: 2016-6-20 . 4 Function: getArrSet . 5 the Param: $ ARRS two-dimensional array . 6 getArrSet (Array (Array (), ...)) . 7 array does not duplicate set of permutations . 8 * / function getArrSet (ARRS $, $ _current_index = - . 1 ) { // total number of groups static $ _total_arr; // total number of groups indexed count static $ _total_arr_index; // array length input static $ _total_count; // makeshift array static $ _temp_arr; // into the input layer of the first array, a static array emptied, and the array length initialization input IF ($ _current_index < 0 ) { $ _total_arr = Array (); $ _total_arr_index = 0 ; $ _temp_arr = Array (); $ _total_count = COUNT ($ ARRS) - . 1 ; $ the this -> getArrSet ($ ARRS, 0 ); } the else { // cycle of $ _current_index group layers the foreach ($ ARRS [$ _ CURRENT_INDEX of] AS $ rKey => $ V) { // if the current cycle is less than the array length of the array input IF (_current_index $ < $ _total_count) { // the current cycle out of the array value into a temporary array $ _temp_arr [$ _ current_index] = $ v.' : ' .. CURRENT_INDEX of $ _ ' , ' . $ RKey; // with continued cycling an array $ the this -> getArrSet (ARRS $, $ + _current_index . 1 ); } // if the current cycle is equal to the array length of the input array ( this array is the final array) the else IF (== $ $ _current_index _total_count) // current value of the loop out of the array into a temporary array { $ _temp_arr [CURRENT_INDEX of $ _] = $ V. ' : ' $ CURRENT_INDEX of _.. ' , ' . $ rKey; //The total number of groups added temporary array $ _total_arr [$ _ total_arr_index] = $ _temp_arr; // total number of groups indexed count + 1'd $ _total_arr_index ++ ; } } } return $ _total_arr; // return value is an array, the array value before the colon is a combination of content, the content after the colon as a combination of the coordinates in the original array }
$arr = array(
array('a', 'b', 'c'),
array('A', 'B', 'C'),
array('I','II','III')
);
$result = $this->getArrSet($arr);
var_dump ($ result);