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,008 questions

51,955 answers

573 users

How to resize an array in PHP

6 Answers

0 votes
function allocate_array($size) {
    $arr = array_fill(0, $size, 0);
    
    return $arr;
}

function resize_array($arr, $new_size) {
    $current_size = count($arr);
    
    if ($new_size > $current_size) {
        for ($i = $current_size; $i < $new_size; $i++) {
            $arr[$i] = 0;
        }
    } elseif ($new_size < $current_size) {
        $arr = array_slice($arr, 0, $new_size);
    }
    
    return $arr;
}

function print_array($arr) {
    foreach ($arr as $value) {
        echo $value . "\n";
    }
}

$arr = allocate_array(3);
$arr[0] = 10;
$arr[1] = 20;
$arr[2] = 30;

print_array($arr);

$arr = resize_array($arr, 5);

echo "\nafter realloc\n";
print_array($arr);

$arr[3] = 40;
$arr[4] = 50;

echo "\nafter realloc & set new data\n";
print_array($arr);



/*
run:

10
20
30

after realloc
10
20
30
0
0

after realloc & set new data
10
20
30
40
50

*/

 



answered Oct 14, 2025 by avibootz
0 votes
$array = [1, 2, 3, 4];

// array_pad(array $array, int $length, mixed $value): array
$resizedArray = array_pad($array, 6, 0); 

print_r($array);
print_r($resizedArray);



/*
run:

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

*/

 



answered Oct 14, 2025 by avibootz
0 votes
$array = [1, 2, 3, 4, 5, 6];

/*
array_splice(
    array &$array,
    int $offset,
    ?int $length = null,
    mixed $replacement = []
): array
*/

$resizedArray = array_splice($array, 0, 2); 

print_r($array);
print_r($resizedArray);



/*
run:

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

*/

 



answered Oct 14, 2025 by avibootz
0 votes
$array = [1, 2, 3, 4, 5, 6];

/*
array_splice(
    array &$array,
    int $offset,
    ?int $length = null,
    mixed $replacement = []
): array
*/

$resizedArray = array_splice($array, 2); 

print_r($array);
print_r($resizedArray);



/*
run:

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

*/

 



answered Oct 14, 2025 by avibootz
0 votes
$array = array("red", "green");
 
print_r(array_pad($array, 6, "blue"));
 
 
 
/*
run:
 
Array
(
    [0] => red
    [1] => green
    [2] => blue
    [3] => blue
    [4] => blue
    [5] => blue
)
 
*/

 



answered Oct 14, 2025 by avibootz
0 votes
$array = array("red", "green");

print_r(array_pad($array, -6, "blue"));



/*
run:

Array
(
    [0] => blue
    [1] => blue
    [2] => blue
    [3] => blue
    [4] => red
    [5] => green
)

*/

 



answered Oct 14, 2025 by avibootz
...