这是我如何在三个数组中创建条目组合的答案。
<?php
function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
// get combinations from subsequent arrays
$tmp = combinations($arrays, $i + 1);
$result = array();
// concat each array from tmp with each element from $arrays[$i]
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ?
array_merge(array($v), $t) :
array($v, $t);
}
}
return $result;
}
$arrCombi=combinations(
array(
array('Microsoft','Adobe','Softskills'),
array('Word','Excel','Photoshop','Homeoffice'),
array('Grundlagen','Expert')
)
);
$line="";
foreach ($arrCombi as $k => $v){
$line .= "(".implode(" AND ",$v) . ") OR \n";
}
$line=substr($line, 0, -4);
echo nl2br($line);
echo"<hr><pre>";
print_r(
combinations(
array(
array('Microsoft','Adobe','Softskills'),
array('Word','Excel','Photoshop','Homeoffice'),
array('Grundlagen','Expert')
)
)
);
echo "</pre>";
?>