You want to modify the size of an array, either by making it larger or smaller than its current size.
Use array_pad() to make an array grow. To reduce an array, use array_splice().
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php echo "<p><b>Use array() to create the array fruits.</b></p>"; $fruits = array('Apples', 'Grapes', 'Bananas', 'Oranges', 'Pears'); // use print_r() to print the array print_r($fruits); echo "<br />the array size is: ", count($fruits); // Use array_pad() echo "<p><b>Use array_pad() to make a six-element array to the right.</b></p>"; $fruits = array_pad($fruits, 6, 'Dates'); print_r($fruits); echo "<br />the array size is: ", count($fruits); // Use array_pad() echo "<p><b>Use array_pad() to make an 8-element array to the left.</b></p>"; $fruits = array_pad($fruits, -8, 'Kiwis'); print_r($fruits); echo "<br />the array size is: ", count($fruits); //Shrink to 4 elements using array_splice() echo "<p><b>Use array_splice() to shrink to 4-element array.</b></p>"; array_splice($fruits, 4); print_r($fruits); echo "<br />the array size is: ", count($fruits); // Remove last element echo "<p><b>Remove last element.</b></p>"; array_splice($fruits, -1); print_r($fruits); echo "<br />the array size is: ", count($fruits); ?> </p> </body> </html> |