Home > Backend Development > PHP Tutorial > 求大神帮忙,这个改怎么处理,php数组组合

求大神帮忙,这个改怎么处理,php数组组合

WBOY
Release: 2016-06-20 12:44:10
Original
1049 people have browsed it

比如我有两个数组 
A组:10,11,12,13
B组:14,15

想组合成 10|14,11|14,12|14,13|14,10|15,11|15,12|15,13|15

拜谢了 可能会发生三组 四组 比如A组 B组 C组 D组


回复讨论(解决方案)

你这是在求笛卡尔积,精华区有好几个相关讨论
这里再给个版本

$a = array(10,11,12,13);$b = array(14,15);foreach(Descartes($a, $b) as $v) $r[] = join(',', $v);echo join('|', $r);
Copy after login
Copy after login
10,14|10,15|11,14|11,15|12,14|12,15|13,14|13,15

  function Descartes($d) {	if(func_num_args() > 1) $d = func_get_args();	$r = array_pop($d);	while($d) {		$t = array();		$s = array_pop($d);		if(! is_array($s)) $s = array($s);		foreach($s as $x) {			foreach($r as $y) $t[] = array_merge(array($x), is_array($y) ? $y : array($y));		}		$r = $t;	}	return $r;  }
Copy after login
Copy after login

你这是在求笛卡尔积,精华区有好几个相关讨论
这里再给个版本

$a = array(10,11,12,13);$b = array(14,15);foreach(Descartes($a, $b) as $v) $r[] = join(',', $v);echo join('|', $r);
Copy after login
Copy after login
10,14|10,15|11,14|11,15|12,14|12,15|13,14|13,15

  function Descartes($d) {	if(func_num_args() > 1) $d = func_get_args();	$r = array_pop($d);	while($d) {		$t = array();		$s = array_pop($d);		if(! is_array($s)) $s = array($s);		foreach($s as $x) {			foreach($r as $y) $t[] = array_merge(array($x), is_array($y) ? $y : array($y));		}		$r = $t;	}	return $r;  }
Copy after login
Copy after login


谢谢版主,我等会测试下,稍后我再发布个需求,等会您再帮我看看

http://bbs.csdn.net/topics/391860657 大神再帮我看看这个

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template