请教二维数组矩阵算法

WBOY
Release: 2016-06-23 13:54:06
Original
902 people have browsed it


$ary = array(	'a'=>array('a1','a2'),	'b'=>array('b1','b2'),	'c'=>array('c1','c2'),);
Copy after login

如何把上面的数组,输出到页面成这样啊


回复讨论(解决方案)

echo '<table width="150px" border="1">';foreach($ary['a'] as $aV){	foreach($ary['b'] as $bV){		foreach($ary['c'] as $cV){			echo "<tr><td>$aV</td><td>$bV</td><td>$cV</td></tr>";		}	}}echo '</table>';
Copy after login
Copy after login

echo '<table width="150px" border="1">';foreach($ary['a'] as $aV){	foreach($ary['b'] as $bV){		foreach($ary['c'] as $cV){			echo "<tr><td>$aV</td><td>$bV</td><td>$cV</td></tr>";		}	}}echo '</table>';
Copy after login
Copy after login


忘了说, 键a,b,c是可能n个的

$ary = array(    'a'=>array('a1','a2'),    'b'=>array('b1','b2'),    'c'=>array('c1','c2'),);echo "<table>\n";echo "<tr><td>a</td><td>b</td><td>c</td></tr>\n";foreach($ary['a'] as $m) {  foreach($ary['b'] as $n) {    foreach($ary['c'] as $v) {      echo "<tr><td>$m</td><td>$n</td><td>$v</td></tr>\n";    }  }}echo "</table>";
Copy after login
[code=html]









abc
a1b1c1
a1b1c2
a1b2c1
a1b2c2
a2b1c1
a2b1c2
a2b2c1
a2b2c2

/code]

精华区有 求笛卡尔积 的代码

精华区有 求笛卡尔积 的代码


忘了说, 键a,b,c是可能n个的,如:a,b,c,d,e,f...................

$ary = array(    'a'=>array('a1','a2'),    'b'=>array('b1','b2'),    'c'=>array('c1','c2'),	'd'=>array('d1','d2'),	'e'=>array('e1','e2'),	//..........多行......);
Copy after login

精华区 迪尔卡积  我问的问题....前后几篇 很完整了

Related labels:
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