Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

//create test data
$data = array(1,2,3);


//add "start" to beginning of array
$data = array_pad($data, -4, "start");


//add "end" to end of array
$data = array_pad($data, 5, "end");


foreach($data as $value)
{
print("$value
\n");
}
?>


value array_pop(array stack)


The array_pop function returns the last element of an array, removing it from the array
as well. The array_push function compliments it, and array_shift and
array_unshift add and remove elements from the beginning of an array.


<?
//set up an array of color names
$colors = array("red", "blue", "green");


$lastColor = array_pop($colors);
//prints "green"
print($lastColor. "
\n");


//shows that colors contains red, blue
print("

");
print_r($colors);
print("
\n");
?>


boolean array_push(array stack, expression entry, ...)


The array_push function adds one or more values to the end of an array. It treats the
array as a stack. Use array_pop to remove elements from the stack. The array_shift
and array_unshift functions add and remove elements to the beginning of an array.


<?
//set up an array of color names
$colors = array("red", "blue", "green");


//push two more color names
array_push($colors, "purple", "yellow");

Free download pdf