<?php
$arr = array(
array(‘A1’,‘A2’),
array(‘B1’,‘B2’),
array(‘C1’,‘C2’)
);
print_r(zuhe($arr,false));
// or
zuhe($arr,true);
/**
* @param: $arr: array for deal
* @param: $echo: print at end or not
*/
function zuhe($arr, $echo = true) {
$len = count($arr);
if ($len == 0 ) return array();
if ($len == 1) {
if ($echo) {
foreach( $arr[0] as $val) {
echo $val ."\n";
}
} else {
return $arr[0];
}
exit();
}
$tmparr = array();
foreach($arr[0] as $val0) {
foreach($arr[1] as $val1) {
$tmparr[] = $val0 ."\t". $val1;
}
}
array_shift($arr);
array_shift($arr);
array_unshift($arr, $tmparr);
return zuhe($arr, $echo);
}
/**
* result:
* A1 B1 C1
* A1 B1 C2
* A1 B2 C1
* A1 B2 C2
* A2 B1 C1
* ….
* A2 B2 C2
*/
?>
实现2:
<?php
$arr = array(
‘gateway’=>array(‘set’,‘noset’),
‘returntype’=>array(‘META’,‘TEXT’,‘TEXT2’),
‘url’=>array(‘login.php’,‘set¬_login.php’,‘noset’),
‘st’=>array(‘ok’,‘err’,‘no’)
);
echo implode("\t\t\t\t",array_keys($arr))."\n"."\n";
//print_r(zuhe($arr,false));
// or
zuhe($arr,true, –20);
/**
* @param: $arr: array for deal
* @param: $echo: print at end or not
*/
function zuhe($arr, $echo = true, $width = "\t") {
$len = count($arr);
if ($len == 0 ) return array();
if ($len == 1) {
if ($echo) {
foreach( $arr[0] as $val) {
echo $val ."\n";
}
} else {
return $arr[0];
}
exit();
}
$tmparr = array();
$arr0 = array_shift($arr);
$arr1 = array_shift($arr);
foreach($arr0 as $val0) {
foreach($arr1 as $val1) {
if ($width == "auto") {
// 希望能自动调整列宽,为实现
}else if ($width == "\t") {
$tmparr[] = $val0 ."\t". $val1;
} else {
$tmparr[] = sprintf("%${width}s",$val0) .sprintf("%${width}s",$val1);
}
}
}
array_unshift($arr, $tmparr);
return zuhe($arr, $echo, $width);
}
/**
* result:
* A1 B1 C1
* A1 B1 C2
* A1 B2 C1
* A1 B2 C2
* A2 B1 C1
* ….
* A2 B2 C2
*/
?>