gpt4 book ai didi

php - 在 PHP 中查找包含二维数组中每一行的一个值的每个元素组合

转载 作者:可可西里 更新时间:2023-11-01 01:11:36 24 4
gpt4 key购买 nike

我在我的项目中使用了更大的数组,但为了简化,假设我们有这个 3x3 数组:

$a = Array( Array(1,2,3),
Array(4,5,6),
Array(7,8,9) );

我想找到每行包含一个值的所有总和组合,即 1+4+7、1+4+8、1+4+9、1+5+7、1+5+8 , 1+5+9, 1+6+7, 1+6+8, 1+6+9, 2+4+7, 2+4+8, 2+4+9, 2+5+7, . ..

希望模式是显而易见的。我首先尝试了一个嵌套循环(列,然后是行),但没有提供所有组合。经过大量搜索后,我确信解决方案需要递归,但每次尝试为此编写递归函数时,我都会感到困惑。

虽然工作代码将非常受欢迎,但对我来说可能更重要的是理解问题和解决方案。

最佳答案

给定数组中未知数量的行和列,获得所需结果的最佳方法是使用递归(否则你将不得不编写任意数量的嵌套循环)。此函数对数组中的每一行进行递归并返回两个结果之一:

  1. 如果我们位于数组的最后一行,则返回该行;
  2. 否则,返回当前行与数组余额结果的叉积(使用两个嵌套的foreach 循环)。我们使用 array_shift获取当前行,并将其从数组中删除。

代码如下:

function find_paths($array) {
if (count($array) == 1) return $array[0];
$output = array();
foreach (array_shift($array) as $v1) {
foreach (find_paths($array) as $v2) {
$output[] = array_merge(array($v1), is_array($v2) ? $v2 : array($v2));
}
}
return $output;
}

使用您的示例数据:

$a = Array( Array(1,2,3),
Array(4,5,6),
Array(7,8,9) );
$combinations = find_paths($a);

该函数执行这一系列操作:

  1. 将第 1 行 (1, 2, 3) 与数组 ((4, 5, 6), (7 , 8, 9));
  2. 第一次递归:将第 2 行 (4, 5, 6) 与函数输出的叉积作为数组 ( (7, 8, 9));
  3. 第二次递归:此时我们在数组(7, 8, 9)中只剩下一行,所以我们返回它;
  4. 第一次递归:计算 (4, 5, 6)(7, 8, 9) 的叉积 = ((4, 7), (4, 8), (4, 9), (5, 7), (5, 8), (5, 9), (6, 7), (6, 8), (6, 9)) 并返回;
  5. (1, 2, 3)((4, 7), (4, 8), (4, 9), (5, 7) , (5, 8), (5, 9), (6, 7), (6, 8), (6, 9)) 并返回。

给出输出:

Array (
[0] => Array ( [0] => 1 [1] => 4 [2] => 7 )
[1] => Array ( [0] => 1 [1] => 4 [2] => 8 )
[2] => Array ( [0] => 1 [1] => 4 [2] => 9 )
[3] => Array ( [0] => 1 [1] => 5 [2] => 7 )
[4] => Array ( [0] => 1 [1] => 5 [2] => 8 )
[5] => Array ( [0] => 1 [1] => 5 [2] => 9 )
[6] => Array ( [0] => 1 [1] => 6 [2] => 7 )
[7] => Array ( [0] => 1 [1] => 6 [2] => 8 )
[8] => Array ( [0] => 1 [1] => 6 [2] => 9 )
[9] => Array ( [0] => 2 [1] => 4 [2] => 7 )
[10] => Array ( [0] => 2 [1] => 4 [2] => 8 )
[11] => Array ( [0] => 2 [1] => 4 [2] => 9 )
[12] => Array ( [0] => 2 [1] => 5 [2] => 7 )
[13] => Array ( [0] => 2 [1] => 5 [2] => 8 )
[14] => Array ( [0] => 2 [1] => 5 [2] => 9 )
[15] => Array ( [0] => 2 [1] => 6 [2] => 7 )
[16] => Array ( [0] => 2 [1] => 6 [2] => 8 )
[17] => Array ( [0] => 2 [1] => 6 [2] => 9 )
[18] => Array ( [0] => 3 [1] => 4 [2] => 7 )
[19] => Array ( [0] => 3 [1] => 4 [2] => 8 )
[20] => Array ( [0] => 3 [1] => 4 [2] => 9 )
[21] => Array ( [0] => 3 [1] => 5 [2] => 7 )
[22] => Array ( [0] => 3 [1] => 5 [2] => 8 )
[23] => Array ( [0] => 3 [1] => 5 [2] => 9 )
[24] => Array ( [0] => 3 [1] => 6 [2] => 7 )
[25] => Array ( [0] => 3 [1] => 6 [2] => 8 )
[26] => Array ( [0] => 3 [1] => 6 [2] => 9 )
)

如果你想得到总和,你可以简单地使用 array_map在数组上,调用 array_sum获取每个元素的总和:

$sums = array_map(function ($v) { return array_sum($v);}, $combinations);

输出:

Array (
[0] => 12
[1] => 13
[2] => 14
[3] => 13
[4] => 14
[5] => 15
[6] => 14
[7] => 15
[8] => 16
[9] => 13
[10] => 14
[11] => 15
[12] => 14
[13] => 15
[14] => 16
[15] => 15
[16] => 16
[17] => 17
[18] => 14
[19] => 15
[20] => 16
[21] => 15
[22] => 16
[23] => 17
[24] => 16
[25] => 17
[26] => 18
)

Demo on 3v4l.org

关于php - 在 PHP 中查找包含二维数组中每一行的一个值的每个元素组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54385865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com