Home  >  Article  >  php教程  >  多个数组求笛卡尔积

多个数组求笛卡尔积

PHP中文网
PHP中文网Original
2016-05-24 12:52:502704browse

开发olap自定义立方体报表用到
同事之前没写出来,网上找一个
网上那个比较简洁
把自己写的分享出来,可能比较粗糙

<?php 
$a = array(&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;);
$b = array(1,2,3);
$c = array(&#39;@&#39;,&#39;#&#39;);
print_r(combineDika($a,$b,$c));
/**
 * 所有数组的笛卡尔积
 * 
 * @param unknown_type $data
 */
function combineDika() {
	$data = func_get_args();
	$cnt = count($data);
	$result = array();
	foreach($data[0] as $item) {
		$result[] = array($item);
	}
	for($i = 1; $i < $cnt; $i++) {
		$result = combineArray($result,$data[$i]);
	}
	return $result;
}

/**
 * 两个数组的笛卡尔积
 * 
 * @param unknown_type $arr1
 * @param unknown_type $arr2
 */
function combineArray($arr1,$arr2) {
	$result = array();
	foreach ($arr1 as $item1) {
		foreach ($arr2 as $item2) {
			$temp = $item1;
			$temp[] = $item2;
			$result[] = $temp;
		}
	}
	return $result;
}

                            

2. [图片] 运行结果.jpg多个数组求笛卡尔积 

   1181.jpg

                     

 以上就是多个数组求笛卡尔积的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn