PHP - 如何将数组插入特定位置?

标签 php arrays

我希望我能解释我的问题

我有一个数组(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/

相关文章:

php - 由于 Configurations.PHP 文件无法访问网站而导致数据库错误

php - 二叉树 child 计数 php mysql

c - 从 C 中的 malloc 数组中删除元素

Javascript 用方括号替换字符串点符号

c++ - 无法让数组正确更新

php - 显示上传进度

php - mysqli 查询结果元素数量正确但值为空

php - Bootstrap CSS 会覆盖主题吗?

c - 如何在 gtk+ 中创建按钮数组

javascript - 从具有两个类的列计算两个总和