我希望我能解释我的问题
我有一个数组(10x10),里面填充了 1 和 0 例如
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000111100
0000000000
1111000000
0000000000
现在我想推送另一个数组
例如
11111
11111
进入特定位置点(3,3)
我已用粗体突出显示了该位置
0000000000
0000000000
0011111000
0011111000
0000000000
0000000000
0000111100
0000000000
1111000000
0000000000
此外,如果该字段上已经有一个值,请添加它,所以如果我重复 处理矩阵就会变成这样。
0000000000
0000000000
0022222000
0022222000
0000000000
0000000000
0000111100
0000000000
1111000000
0000000000
我希望有人能指出我正确的方向。我已经使用过一些数组函数,但我无法让它工作。
你有什么建议?
最佳答案
按照这些思路应该可以做到:
/**
* Push a small matrix into a larger matrix.
* Note: no size restrictions are applied, if $push
* exceeds the size of $matrix or the coordinates are
* too large you'll get a jaggy array.
*
* @param array $matrix A 2D array of ints.
* @param array $push A 2D array <= $matrix to push in.
* @param int $x The starting x coordinate.
* @param int $y The starting y coordinate.
* @return array The modified $matrix.
*/
function matrix_push(array $matrix, array $push, $x, $y) {
list($i, $j) = array($x, $y);
foreach ($push as $row) {
foreach ($row as $int) {
$matrix[$i][$j++] += $int;
}
$i++;
$j = $y;
}
return $matrix;
}
关于PHP - 如何将数组插入特定位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17492135/