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

(singke) #1

Compare this function to array_merge and array_slice.


<?
function printElement($element)
{
print("$element
\n");
}


//set up an array of color names
$colors = array("red", "blue", "green",
"yellow", "orange", "purple");


//remove green
array_splice($colors, 2, 2);


//insert "pink" after "blue"
array_splice($colors, 2, 0, "pink");


//insert "cyan" and "black" between
//"orange" and "purple"
array_splice($colors, 4, 0, array("cyan", "black"));


//print out all the values
array_walk($colors, "printElement");
?>


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


The array_unshift function adds one or more values to the beginning of an array, as if
the array were a stack. Use array_shift to remove an element from the beginning of an
array. Compare this function to array_pop and array_push, which operate on the end of
the array.


<?
function printElement($element)
{
print("$element
\n");
}


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


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


//print out all the values
array_walk($colors, "printElement");
?>

Free download pdf