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 array with predefined size in PHP

2 Answers

0 votes
$size = 10; 
$array = array_fill(0, $size, NULL);

print_r($array);




/*
run:

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

*/

 



answered Jan 23, 2021 by avibootz
0 votes
$arr = array();
$arr = array_fill(0, 10, 0);

print_r($arr);
 
$arr[3] = 99;
$arr[7] = 14;

echo "\n\n";
print_r($arr); 

 
     
/*
run:
 
Array
(
    [0] => 0
    [1] => 0
    [2] => 0
    [3] => 0
    [4] => 0
    [5] => 0
    [6] => 0
    [7] => 0
    [8] => 0
    [9] => 0
)


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

*/

 



answered Apr 12, 2024 by avibootz

Related questions

2 answers 231 views
1 answer 78 views
1 answer 148 views
1 answer 147 views
3 answers 419 views
419 views asked Apr 14, 2018 by avibootz
1 answer 179 views
1 answer 156 views
...