1 回答
TA贡献1836条经验 获得超4个赞
你需要将你的数组转换成这个,
$a = ["abc", "defs", "ghi"];
$b = ["abcs", "def", "ghis"];
$temp = array_map(null, $a, $b); // this conversion we call it transposing of array
function combinations($arrays)
{
$result = [];
$arrays = array_values($arrays);
$sizeIn = sizeof($arrays);
$size = $sizeIn > 0 ? 1 : 0;
foreach ($arrays as $array) {
$size = $size * sizeof($array);
}
for ($i = 0; $i < $size; $i++) {
$result[$i] = [];
for ($j = 0; $j < $sizeIn; $j++) {
array_push($result[$i], current($arrays[$j]));
}
for ($j = ($sizeIn - 1); $j >= 0; $j--) {
if (next($arrays[$j])) {
break;
} elseif (isset($arrays[$j])) {
reset($arrays[$j]);
}
}
}
return $result;
}
$res = combinations($temp);
// imploding all the values internally with space
$temp = array_map(function($item){
return implode(" ", $item);
},$res);
// looping to show the data
foreach($temp as $val){
echo $val."\n";
}
使用 array_map 转换数组后,我使用了this 的帮助。
演示。
输出
abc defs ghi
abc defs ghis
abc def ghi
abc def ghis
abcs defs ghi
abcs defs ghis
abcs def ghi
abcs def ghis
- 1 回答
- 0 关注
- 246 浏览
添加回答
举报