如何在PHP中生成具有不同元素编号的多个数组中的所有项组合
我试图在几个数组中找到所有项的组合。阵列的数量是随机的(可以是2、3、4、5…)。每个数组中的元素数也是随机的 例如,我有3个数组:如何在PHP中生成具有不同元素编号的多个数组中的所有项组合,php,arrays,combinations,Php,Arrays,Combinations,我试图在几个数组中找到所有项的组合。阵列的数量是随机的(可以是2、3、4、5…)。每个数组中的元素数也是随机的 例如,我有3个数组: $arrayA = array('A1','A2','A3',,'A4','A5','A6'); // (3 From here each time) $arrayB = array('B1','B2','B3','B4','B5'); //(2 From here each time) $arrayC = array('C1','C2'); // (1 Fr
$arrayA = array('A1','A2','A3',,'A4','A5','A6'); // (3 From here each time)
$arrayB = array('B1','B2','B3','B4','B5'); //(2 From here each time)
$arrayC = array('C1','C2'); // (1 From here each time)
我想生成如下数组:
A1+A3+A5, B1+B5, C1
A1+A4+A5, B1+B4, C1
....
我试过这样做,但我需要不同的元素数从数组,而不是一个元素从每个数组
<?php
$color = array('Blue','Red','Black','Green');
$size = array('L','M','S','XL','XXL');
$type = array('Half selevs','full seleves');
$options = [
$color,
$size,
$type,
];
$combinations = getCombinations($options);
print_r($combinations);
function getCombinations($options){
$combinations = [[]];
for ($count = 0; $count < count($options); $count++) {
$tmp = [];
foreach ($combinations as $v1) {
foreach ($options[$count] as $v2)
$tmp[] = array_merge($v1, [$v2]);
}
$combinations = $tmp;
}
return $combinations;
}
?>
)
我想要这样:
排列
(
[0]=>阵列
(
[0]=>蓝色、红色、绿色
[1] =>L,M
[2] =>半selevs
)你可以这样做
<?php
$variants = array();
$possible_letters = array('A','B','C');
$max_variant_number = 6;
foreach ($possible_letters as $possible_letter) {
$min_variant_number = 0;
while ($min_variant_number <= $max_variant_number) {
$variants[] = $possible_letters.$min_variant_number;
$min_variant_number++;
}
}
似乎您需要一个数组组合函数。我在这里找到了一个解决方案:
我在这里复制您的实用程序的代码,有关更多详细信息,请使用上面的链接
class Combinations implements Iterator
{
protected $c = null;
protected $s = null;
protected $n = 0;
protected $k = 0;
protected $pos = 0;
function __construct($s, $k) {
if(is_array($s)) {
$this->s = array_values($s);
$this->n = count($this->s);
} else {
$this->s = (string) $s;
$this->n = strlen($this->s);
}
$this->k = $k;
$this->rewind();
}
function key() {
return $this->pos;
}
function current() {
$r = array();
for($i = 0; $i < $this->k; $i++)
$r[] = $this->s[$this->c[$i]];
return is_array($this->s) ? $r : implode('', $r);
}
function next() {
if($this->_next())
$this->pos++;
else
$this->pos = -1;
}
function rewind() {
$this->c = range(0, $this->k);
$this->pos = 0;
}
function valid() {
return $this->pos >= 0;
}
protected function _next() {
$i = $this->k - 1;
while ($i >= 0 && $this->c[$i] == $this->n - $this->k + $i)
$i--;
if($i < 0)
return false;
$this->c[$i]++;
while($i++ < $this->k - 1)
$this->c[$i] = $this->c[$i - 1] + 1;
return true;
}
}
//You must call this function foreach array you have
//with the number of elements that you want to combine
//as second parameter. Example:
$arrayA = array('A1','A2','A3','A4','A5','A6');
foreach(new Combinations($arrayA, 3) as $subarray){
foreach($subarray as $key => $value){
echo $value." ";
}
echo "\r\n";
}
链接以测试它:
你的意思是,你真的想对组合的元素求和/求和?不确定你在示例结果中所说的+
是什么意思。你真的尝试过什么吗,或者你只是要求我们为你做所有的工作?我们想看看你为解决问题所做的努力。很抱歉,我在那里添加了我的代码。
class Combinations implements Iterator
{
protected $c = null;
protected $s = null;
protected $n = 0;
protected $k = 0;
protected $pos = 0;
function __construct($s, $k) {
if(is_array($s)) {
$this->s = array_values($s);
$this->n = count($this->s);
} else {
$this->s = (string) $s;
$this->n = strlen($this->s);
}
$this->k = $k;
$this->rewind();
}
function key() {
return $this->pos;
}
function current() {
$r = array();
for($i = 0; $i < $this->k; $i++)
$r[] = $this->s[$this->c[$i]];
return is_array($this->s) ? $r : implode('', $r);
}
function next() {
if($this->_next())
$this->pos++;
else
$this->pos = -1;
}
function rewind() {
$this->c = range(0, $this->k);
$this->pos = 0;
}
function valid() {
return $this->pos >= 0;
}
protected function _next() {
$i = $this->k - 1;
while ($i >= 0 && $this->c[$i] == $this->n - $this->k + $i)
$i--;
if($i < 0)
return false;
$this->c[$i]++;
while($i++ < $this->k - 1)
$this->c[$i] = $this->c[$i - 1] + 1;
return true;
}
}
//You must call this function foreach array you have
//with the number of elements that you want to combine
//as second parameter. Example:
$arrayA = array('A1','A2','A3','A4','A5','A6');
foreach(new Combinations($arrayA, 3) as $subarray){
foreach($subarray as $key => $value){
echo $value." ";
}
echo "\r\n";
}
A1 A2 A3
A1 A2 A4
A1 A2 A5
A1 A2 A6
A1 A3 A4
A1 A3 A5
A1 A3 A6
A1 A4 A5
A1 A4 A6
A1 A5 A6
A2 A3 A4
A2 A3 A5
A2 A3 A6
A2 A4 A5
A2 A4 A6
A2 A5 A6
A3 A4 A5
A3 A4 A6
A3 A5 A6
A4 A5 A6