0

I am wondering if there is an easier way to do the same thing than below but with less code.

    private $table = array(
        array(array(), array(), array(), array(), array()),
        array(array(), array(), array(), array(), array()),
        array(array(), array(), array(), array(), array()),
                               ...
        array(array(), array(), array(), array(), array())
    );

I know this is doable in C++, tho not sure if there is something similar in PHP.

Thanks in advance for the help.

2 Answers 2

4

You could use array_fill

$a = array_fill( 0, 4, array_fill( 0, 5, array() ));
print_r( $a );
Array
(
    [0] => Array
        (
            [0] => Array
                (
                )

            [1] => Array
                (
                )

            [2] => Array
                (
                )

            [3] => Array
                (
                )

            [4] => Array
                (
                )

        )

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

            [1] => Array
                (
                )

            [2] => Array
                (
                )

            [3] => Array
                (
                )

            [4] => Array
                (
                )

        )

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

            [1] => Array
                (
                )

            [2] => Array
                (
                )

            [3] => Array
                (
                )

            [4] => Array
                (
                )

        )

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

            [1] => Array
                (
                )

            [2] => Array
                (
                )

            [3] => Array
                (
                )

            [4] => Array
                (
                )

        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

same here, im glad i asked ;)
2

If this is a straight rectangle of known size, you might use nested loops:

$table = array();
for ($row = 0; $row < $rowCount; $row++)
{
  $thisRow = array ();
  for ($col = 0; $col < $colCount; $col++)
  {
    $thisRow[] = array ();
  }
  $table[] = $thisRow;
}

I know it's not exactly what you asked, but at least it'd be more resistant to change (like adding a column) than your current setup.

1 Comment

thanks, i appreciate your help, but like you said, it's not what i am looking for (i am actually running away from loops and "specific" assignments at each index...)

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.