Jan
Jan

Reputation: 55

How to declare a 100x100 matrix in PHP

How can I declare a matrix having 100 rows and 100 columns in PHP?

Here is what I already know:

I know how to do it for 3x3:

$matrix=array(
    array(1,2,3),
    array(4,5,6),
    array(7,8,9)  
);

Or:

$matrix=[
    [1,2,3],
    [4,5,6],
    [7,8,9]
];

And then I have found a way of adding elements to a one dimensional array:

$t7=array("white","blue");
array_push($t7,"green","orange");

Thank you.

Upvotes: 1

Views: 167

Answers (4)

Maik Lowrey
Maik Lowrey

Reputation: 17586

Because there are already two For loops in answers :-), I thought of using a while and the PHP Range function. But the For loop solution would also be my first choice.

$y = 0;
$matrix = [];
$range = 3;

while($y < $range) {
  $matrix[$y] = range($start = $start ?? 1, $start + ($range - 1));
  $start += $range;
  $y++;
}
print_r($matrix);

// ----
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [2] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )
)

Upvotes: 1

0stone0
0stone0

Reputation: 44073

Since you trying to define a 'square' matrix, we can use that to create a simple loop using range to get the required numbers:

<?php

$x = 5; // 100
$m = [];

for ($i = 0; $i < $x; $i++) {
    $m[] = range($i * $x, $i * $x + $x - 1);
}

var_dump($m);

Here we create an empty array, loop n times, add a new array to the original one starting at the previous value, until + 1 $y

This will generate:

array(5) {
  [0]=>
  array(5) {
    [0]=>
    int(0)
    [1]=>
    int(1)
    [2]=>
    int(2)
    [3]=>
    int(3)
    [4]=>
    int(4)
  }
  [1]=>
  array(5) {
    [0]=>
    int(5)
    [1]=>
    int(6)
    [2]=>
    int(7)
    [3]=>
    int(8)
    [4]=>
    int(9)
  }
... and some more

As you can test in this online demo

Upvotes: 0

KrassVerpeilt
KrassVerpeilt

Reputation: 445

As always, there are several options. I decided to use the approach with two For loops. With the limiter ($limit) you set the matrix depth.

<?php
$matrix = [];
$limit = 3;
$counter = 1;
for ($i = 0; $i < $limit; $i++){
  $arr = [];
  for($j = 0; $j < $limit; $j++) {
    $arr[] = $counter++;
  }
  $matrix[$i] = $arr;  
}

print_r($matrix);

output:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [2] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

)

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94672

You can do it with a couple of for loops, this is done only to a 5 x 5 matrix but just change the height and width to make it any size you like

$width = 5;
$height = 5;
$x = 1;

$matrix = [];

for( $i=0; $i < $width; $i++) {
    for( $j=0; $j < $height; $j++) {
        $matrix[$i][] = $x++;
    }
}

print_r($matrix);

RESULT

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

Upvotes: 3

Related Questions