2

I hope I can explain my problem

I have on array(10x10) filled with 1 and 0 e.g.

0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000111100
0000000000
1111000000
0000000000

Now I'd like to push another array

e.g.

11111
11111

into it on a specific position point (3,3)
I've highlighted the position in bold

0000000000
0000000000
0011111000
0011111000
0000000000
0000000000
0000111100
0000000000
1111000000
0000000000

Futhermore, if there is already a value on the field add it, so if I repeat the process the matrix will become this.

0000000000
0000000000
0022222000
0022222000
0000000000
0000000000
0000111100
0000000000
1111000000
0000000000

I hope someone can point me in the right direction. I've already played with some array functions, but I can't get it to work.

What do you suggest?

2
  • 1
    Could you show us what array functions you have tried, and what exactly didn't work? Commented Jul 5, 2013 at 15:20
  • Just start at $arr[$i][$j], loop through your array-to-push while incrementing $j and $i as appropriate. It's pretty simple, really. Commented Jul 5, 2013 at 15:21

3 Answers 3

2

Something along these lines should do it:

/**
 * 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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Can you explain the first line of the function? Why not just use x and y?
@jcsanyi Because I need the original $y later on. Admittedly $i is redundant, but more explicit.
Thanks for that fast answer
Maybe you can edit in the safety check from my answer, as you can extend rows, or create an index error by calling a non-existing row with your function.
1

For a 2-dimensional array you could do something like this. Please note that what you define as (3,3) in your question, is actually (2,2) from an array perspective. The input $x and $y would be 2 and 2 in this case.

function replaceArray( $orig, $replace, $x, $y ) {
  //Some safety checks
  //Return false if the array that we replace with exceeds the boundaries of the original array
  if( $x + count( $replace[0] ) > count( $orig[0] ) || $y + count( $replace ) > count( $orig ) ) {
    return false;
  }

  for( $i = 0; $i < count($replace[0]); $i++ ) {
    for( $j = 0; $j < count( $replace ); $j++ ) {
      $orig[ $x + $i ][ $x + $j ] += $replace[ $i ][ $j ];
    }
  }

  return $orig;
}

Comments

0

Wow thats was fast :)

Nice way you've implemented it. Here is my code that solve it too, but with some line more

$array1 = array(0=>array(0,0,0,0,0,0,0,0,0,0,0),
        1=>array(0,0,0,0,0,0,0,0,0,0,0),
        2=>array(0,0,0,0,0,0,0,0,0,0,0),
        3=>array(0,0,0,0,0,0,0,0,0,0,0),
        4=>array(0,0,0,0,0,0,0,0,0,0,0),
        5=>array(0,0,0,0,0,0,0,0,0,0,0),
        6=>array(0,0,0,0,0,0,0,0,0,0,0),
        7=>array(0,0,0,0,0,0,0,0,0,0,0));



$array2 = array(0=>array(1,1,1),
                1=>array(1,1,1),
                2=>array(1,1,1));

$row = 3;
$col = 3;
foreach($array2 AS $valuesToReplace)
{   $col = 3;
    foreach($valuesToReplace AS $value)
    {
        $array1[$row][$col] = $value;
        $col++;
    }
    $row++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.