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.

40,026 questions

51,982 answers

573 users

How to pad an array to a specified length with a given value in PHP

1 Answer

0 votes
$arr = array(1, 2, 3);
$arr_1 = array_pad($arr, 6, 0);
print_r($arr_1); 
echo "\n";
 
$arr = array(1, 2, 3, 4);
$arr_1 = array_pad($arr, 7, 10);
print_r($arr_1); 
echo "\n";
 
$arr = array(1, 2, 3);
$arr_1 = array_pad($arr, -6, -3);
print_r($arr_1); 
echo "\n";
 

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

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

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


answered Apr 21, 2014 by avibootz
edited Feb 4, 2025 by avibootz
...