Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to create an empty 2D array in PHP

3 Answers

0 votes
$emptyArray = array(array());

print_r($emptyArray);




/*
run:

Array
(
    [0] => Array
        (
        )

)

*/

 



answered Apr 14, 2018 by avibootz
edited Nov 3, 2023 by avibootz
0 votes
$emptyArray = [[]];
 
print_r($emptyArray);
 
 
 
 
/*
run:
 
Array
(
    [0] => Array
        (
        )
 
)
 
*/

 



answered Apr 14, 2018 by avibootz
edited Nov 3, 2023 by avibootz
0 votes
$arr2d = [[]];

print_r($arr2d);

$rows = 2;
$cols = 3;

for ($i = 0; $i < $rows; $i++)
    for ($j = 0; $j < $cols; $j++)
        $arr2d[$i][$j] = rand(2, 9);
      
echo "\n\n";
print_r($arr2d);
 
 
     
/*
run:
 
Array
(
    [0] => Array
        (
        )

)


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

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

)

*/

 



answered Apr 12, 2024 by avibootz

Related questions

1 answer 76 views
76 views asked Jan 10, 2025 by avibootz
2 answers 327 views
1 answer 135 views
1 answer 173 views
1 answer 150 views
150 views asked Sep 19, 2020 by avibootz
1 answer 156 views
...