Last active
December 18, 2015 04:39
-
-
Save lokielse/5726970 to your computer and use it in GitHub Desktop.
some iteration func
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by sqiu. | |
* CreateTime: 13-6-6 下午7:18 | |
* | |
*/ | |
$src = array(); | |
$src[] = array('A','B','C','D','E'); | |
$src[] = array('+', '-', '*', '/'); | |
$src[] = array(1,2,3); | |
$combine = iteration_get_combine($src); | |
echo '<pre>'; | |
print_r($combine); | |
echo '</pre>'; | |
/** | |
* @param $src | |
* @param array $treenode | |
* @param int $src_index | |
* @param array $strarr | |
* | |
* @return array|string | |
*/ | |
function iteration_gen_tree_witch_each_array($src, $treenode = array(), $src_index=0, $strarr=array()){ | |
if(!isset($src[$src_index])) return ''; | |
foreach($src[$src_index] as $nodekey){ | |
$treenode[$nodekey] = iteration_gen_tree_witch_each_array($src, $strarr[$nodekey], $src_index+1, $strarr); | |
} | |
return $treenode; | |
} | |
/** | |
* @param $node | |
* @param $tree | |
* @param array $pk | |
*/ | |
function iteration_convert_to_one_node_from_tree(&$node, $tree, $pk=array()){ | |
if($node===NULL) $node = array(); | |
foreach($tree as $k=>$item){ | |
$_pk = $pk; | |
array_push($_pk, $k); | |
if(is_array($item)){ | |
iteration_convert_to_one_node_from_tree($node, $item, $_pk); | |
}else{ | |
array_push($node, $_pk); | |
} | |
} | |
} | |
/** | |
* @param $array | |
* | |
* @return array | |
*/ | |
function iteration_join_each_node($node){ | |
$ret = array(); | |
foreach($node as $arr){ | |
$ret[] = implode(',', $arr); | |
} | |
return $ret; | |
} | |
/** | |
* @param $src | |
* | |
* @return array | |
*/ | |
function iteration_get_combine($src){ | |
$tree = iteration_gen_tree_witch_each_array($src); | |
iteration_convert_to_one_node_from_tree($node, $tree); | |
$combine = iteration_join_each_node($node); | |
return $combine; | |
} | |
//result below | |
/* | |
Array | |
( | |
[0] => A,+,1 | |
[1] => A,+,2 | |
[2] => A,+,3 | |
[3] => A,-,1 | |
[4] => A,-,2 | |
[5] => A,-,3 | |
[6] => A,*,1 | |
[7] => A,*,2 | |
[8] => A,*,3 | |
[9] => A,/,1 | |
[10] => A,/,2 | |
[11] => A,/,3 | |
[12] => B,+,1 | |
[13] => B,+,2 | |
[14] => B,+,3 | |
[15] => B,-,1 | |
[16] => B,-,2 | |
[17] => B,-,3 | |
[18] => B,*,1 | |
[19] => B,*,2 | |
[20] => B,*,3 | |
[21] => B,/,1 | |
[22] => B,/,2 | |
[23] => B,/,3 | |
[24] => C,+,1 | |
[25] => C,+,2 | |
[26] => C,+,3 | |
[27] => C,-,1 | |
[28] => C,-,2 | |
[29] => C,-,3 | |
[30] => C,*,1 | |
[31] => C,*,2 | |
[32] => C,*,3 | |
[33] => C,/,1 | |
[34] => C,/,2 | |
[35] => C,/,3 | |
[36] => D,+,1 | |
[37] => D,+,2 | |
[38] => D,+,3 | |
[39] => D,-,1 | |
[40] => D,-,2 | |
[41] => D,-,3 | |
[42] => D,*,1 | |
[43] => D,*,2 | |
[44] => D,*,3 | |
[45] => D,/,1 | |
[46] => D,/,2 | |
[47] => D,/,3 | |
[48] => E,+,1 | |
[49] => E,+,2 | |
[50] => E,+,3 | |
[51] => E,-,1 | |
[52] => E,-,2 | |
[53] => E,-,3 | |
[54] => E,*,1 | |
[55] => E,*,2 | |
[56] => E,*,3 | |
[57] => E,/,1 | |
[58] => E,/,2 | |
[59] => E,/,3 | |
) | |
count: 5*4*3 | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment