PHP计数问题算法
我需要得到一组值的所有组合和排列。请参见代码段示例:PHP计数问题算法,php,algorithm,Php,Algorithm,我需要得到一组值的所有组合和排列。请参见代码段示例: $a = array( 1, 2 ); $b = array( 'foo', 'bar' ); $params = array(); $params[] = $a; $params[] = $b; // What to do to $params so I can get the following combinations/permutations? // 1,foo // 2,foo // 1,ba
$a = array(
1,
2
);
$b = array(
'foo',
'bar'
);
$params = array();
$params[] = $a;
$params[] = $b;
// What to do to $params so I can get the following combinations/permutations?
// 1,foo
// 2,foo
// 1,bar
// 2,bar
// foo,1
// bar,1
// foo,2
// bar,2
请记住,
$params
可以是任何大小,其中的项目也可以是任何大小。尝试一下,希望这有帮助
$a = array(
1,
2,
4
);
$b = array(
'foo',
'bar',
'zer'
);
$c = array(
'aaa',
'bbb'
);
$params = array();
$params[] = $a;
$params[] = $b;
$params[] = $c;
$sizeofp = count($params);
for($i=0 ; $i < $sizeofp ; $i++){
foreach ($params[$i] as $paramA) {
for($j=0 ; $j < $sizeofp ; $j++){
if($j == $i){
continue;
}
foreach($params[$j] as $paramB){
echo "\n".$paramA.",".$paramB;
}
}
}
}
$a=数组(
1.
2.
4.
);
$b=数组(
"福",,
“酒吧”,
“泽尔”
);
$c=数组(
“aaa”,
“bbb”
);
$params=array();
$params[]=$a;
$params[]=$b;
$params[]=$c;
$sizeofp=计数($params);
对于($i=0;$i<$sizeofp;$i++){
foreach($params[$i]作为$paramA){
对于($j=0;$j<$sizeofp;$j++){
如果($j=$i){
继续;
}
foreach($params[$j]作为$paramB){
回显“\n”。$paramA.”,“$paramB;
}
}
}
}
注意:如果可能存在重复项(数组包含相同的值),则可以在插入$result之前检查重复项。以下是我的解决方案。它应该适用于任意数量的关联数组,即使它们包含嵌套的关联数组
<?php
$a = array(1,2,3);
$b = array('foo','bar','baz', array('other','other2',array('other3','other4')));
$params = array();
$params[] = $a;
$params[] = $b;
$elements = array();
foreach($params as $param) {
addElement($param,$elements);
}
function addElement($arg,&$result) {
if(!is_array($arg)) {
$result[] = $arg;
} else {
foreach($arg as $argArray) {
addElement($argArray,$result);
}
}
}
for($i=0; $i<count($elements); $i++) {
$curElement = $elements[$i];
for($j=0; $j<count($elements); $j++) {
if($elements[$j] != $curElement) {
$final_results[] = $curElement.','.$elements[$j];
}
}
}
print_r($final_results);
?>
这里有一个可能的解决方案
我认为最好是根据a和b的大小计算出一个排列数的公式,而不是通过计算排列数来处理这个问题。或者你真的需要这些电视机吗?没关系。我现在明白了,您不仅仅是在寻找计数。还可以看到$params的大小是未知的。这假设$params包含2个元素。不,这假设$params可能是无限的。试试看!事实上,不仅$params可能是无止境的。添加到$params的每个$a、$b都可以有任意大小。我将把测试源添加到我的回复中。啊!我明白了,你需要把所有可能的组合都组合起来!那么@yi_H的回复就可以了。yi_H-这看起来很有希望。我会测试的。我不知道你说的复制品是什么意思。你是说如果$a的值为1,而$b的值也为1,那么就存在重复项吗?如果是这样,出于我的目的,它们不会被视为重复。这将为您提供一个数组
$final\u results
,其中包含所有排列和组合
$a = array(1, 2, 3);
$b = array('foo', 'bar');
$c = array('a', 'b');
$params = array($a, $b, $c);
$result = array();
all($params, array(), $result);
print_r($result);
<?php
$a = array(1,2,3);
$b = array('foo','bar','baz', array('other','other2',array('other3','other4')));
$params = array();
$params[] = $a;
$params[] = $b;
$elements = array();
foreach($params as $param) {
addElement($param,$elements);
}
function addElement($arg,&$result) {
if(!is_array($arg)) {
$result[] = $arg;
} else {
foreach($arg as $argArray) {
addElement($argArray,$result);
}
}
}
for($i=0; $i<count($elements); $i++) {
$curElement = $elements[$i];
for($j=0; $j<count($elements); $j++) {
if($elements[$j] != $curElement) {
$final_results[] = $curElement.','.$elements[$j];
}
}
}
print_r($final_results);
?>
$array = array(
0 => array(
'foo',
'bar'
),
1 => array(
1,
2,
3
),
2 => array(
'x',
'y',
'z'
),
3 => array(
7,
8,
9
)
);
array_permutations($array, $permutations);
print_r($permutations);
public function array_permutations($array, &$permutations, $current_key = 0, $current_subkey = 0)
{
if(!isset($array[$current_key][$current_subkey]))
{
return;
}
$current_val = $array[$current_key][$current_subkey];
foreach($array as $array_key => $row)
{
foreach($row as $row_key => $sub_val)
{
if($array_key === $current_key)
{
if($row_key !== $current_subkey)
{
$permutations[] = $current_val . ', ' . $sub_val;
}
}
else
{
$permutations[] = $current_val . ', ' . $sub_val;
}
}
}
$next_key = ($current_subkey == (count($array[$current_key]) - 1)) ? $current_key + 1 : $current_key;
$next_subkey = ($next_key > $current_key) ? 0 : $current_subkey + 1;
array_permutations($array, $permutations, $next_key, $next_subkey);
}