Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an empty 2D array in PHP?

I know that arrays are created dynamically, and creating them ahead of time isn't really necessary, but how would one do that with a 2D array? The same way?

(for$j) { for($i)     {     $array[j][i] = "data";     } } 

Something like that? Obviously real for loops, of course.

like image 299
Joshua Avatar asked Jul 07 '11 15:07

Joshua


People also ask

How do you create an empty array?

1) Assigning it to a new empty array This is the fastest way to empty an array: a = []; This code assigned the array a to a new empty array. It works perfectly if you do not have any references to the original array.

What is a 2 dimensional array PHP?

PHP - Two-dimensional Arrays A two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays of arrays).


1 Answers

At its absolute simplest, a 2D dimensional array can be created as:

<?php     $emptyArray = array(array()); ?> 

Or as of PHP 5.4 you can also use:

<?php     $emptyArray = [[]]; ?> 
like image 88
Brendan Bullen Avatar answered Oct 14 '22 07:10

Brendan Bullen