Home > Blockchain >  PHP: How to create a 2d array that counts up through the inner arrays
PHP: How to create a 2d array that counts up through the inner arrays

Time:02-06

my code creates this:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
            [9] => 10
        )

    [1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
            [9] => 10
        )

    [2] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
            [9] => 10
        )

    [3] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
            [9] => 10
        )
)

but I want this

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
            [9] => 10
        )

    [1] => Array
        (
            [0] => 11
            [1] => 12
            [2] => 13
            [3] => 14
            [4] => 15
            [5] => 16
            [6] => 17
            [7] => 18
            [8] => 19
            [9] => 20
        )

    [2] => Array
        (
            [0] => 21
            [1] => 22
            [2] => 23
            [3] => 24
            [4] => 25
            [5] => 26
            [6] => 27
            [7] => 28
            [8] => 29
            [9] => 30
        )

    [3] => Array
        (
            [0] => 31
            [1] => 32
            [2] => 33
            [3] => 34
            [4] => 35
            [5] => 36
            [6] => 37
            [7] => 38
            [8] => 39
            [9] => 40
        )
)

This is my code:

<?php

require 'vendor/autoload.php';
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

$loader = new FilesystemLoader(__DIR__ . '/templates');
$twig = new Environment($loader);

$rowCount =4;
$columnCount = 10;

$grid = array();
$row = array();
$column = array();

for ($n=1; $n<=$columnCount;$n  ){
    array_push($row, $n);
}

for($i=1; $i<=$rowCount; $i  ){
    array_push($grid, $row);
}

echo "<pre>";
print_r($grid);
echo "<pre>";

echo $twig->render('view.html', array(
    'grid' => $grid,
))
?>

I think I have to put one for loop in the other, i just did it this way for now so you can understand the problem better.

PS: The view.html which is rendered is empty, i want to give it the 2d array later so it creates a grid out of the array with an id for each cell.

If the question is stupid im sorry, but be sure i tried it for myself long enough and it didnt work out.

With friendly regards,

Strambo

CodePudding user response:

On each row you should to fill column. And then put (array_push()) row in the grid. When you filling the column you should increase the column counter (value in your cell).

$rowCount = 4;
$columnCount = 10;

$grid = [];
$currentCollumn = 1;

for($i = 1; $i <= $rowCount; $i  ){
    $row = [];
    for($j = 0; $j < $columnCount; $j  ) {
       $row[$j] = $currentCollumn;
       $currentCollumn  ;
    }
   array_push($grid, $row);
 }

 echo "<pre>";
 print_r($grid);
 echo "<pre>";

P.S.: There are no stupid questions, it's ok question :)

CodePudding user response:

The main issue you can see is that your row is getting created one time -- in that first loop. Then you're taking the same copy of the row and pushing it onto the grid rowCount of times.

So yes, two main ideas here:

  • Any time you have some kind of "grid" or "matrix" problem you're probably looking to use a nested loop, as you suspected
  • You don't want to re-initialize your $currentValue (the number you're placing in every point in the grid) every time -- you only want to initialize it once, and then have it maintain a single incrementing count throughout the grid -- not resetting itself, since you want sequential numbers all the way through

Last tip here is that when you start to get muddled or confused dealing with loops, it helps to rename your counter values from $i/$n to something more descriptive to help you keep track of what is happening where

That gives us this code:

<?php

$rowCount = 4;
$columnCount = 10;

$grid = array();
$row = array();
$column = array();
$currentValue = 1; // we start at 1 and want to count to 40, not re-initialize this in the loop

for ($currentRow = 1; $currentRow <= $rowCount; $currentRow  ) {
    $row = array();

    // first section in for loop is empty since we already initialized $currentValue outside the loops
    for (; $currentValue <= ($columnCount * $currentRow); $currentValue  ) {
        array_push($row, $currentValue);
    }

    array_push($grid, $row);
}

print_r($grid);

Which will print

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
            [9] => 10
        )

    [1] => Array
        (
            [0] => 11
            [1] => 12
            [2] => 13
            [3] => 14
            [4] => 15
            [5] => 16
            [6] => 17
            [7] => 18
            [8] => 19
            [9] => 20
        )

    [2] => Array
        (
            [0] => 21
            [1] => 22
            [2] => 23
            [3] => 24
            [4] => 25
            [5] => 26
            [6] => 27
            [7] => 28
            [8] => 29
            [9] => 30
        )

    [3] => Array
        (
            [0] => 31
            [1] => 32
            [2] => 33
            [3] => 34
            [4] => 35
            [5] => 36
            [6] => 37
            [7] => 38
            [8] => 39
            [9] => 40
        )

)
  •  Tags:  
  • Related