shadowline
The array_push( ) command is used to add a new element to the end of an array. It will look at the current array, find the highest known index number, and add the new item to the array.

<?php
$pantry = array(
1 => "tomatoes",
0 => "oranges",
4 => "bananas",
3 => "potatoes",
2 => "bread"
);

array_push($pantry, "apples");
?>

Even though the array was declared out of order, the push command will find the highest index number and assing the next one to the new array entry. Thus $pantry[5] carries the value of apples. This command works exactly the same as adding in a new value normally with $pantry[]="apples"; This array now lists as...

oranges
tomatoes
bread
potatoes
bananas
apples

The array_pop( ) command is used to delete the last found array entry. This is not looking for the highest index number, but rather the last physical entry for the array.

<?php
$pantry = array(
1 => "tomatoes",
0 => "oranges",
4 => "bananas",
3 => "potatoes",
2 => "bread"
);

array_pop($pantry);
?>

Even though 4 is the highest index number and should be condidered as the last item in the array, it is the number 2 index value that will be deleted as it is the last physical entry shown for the array. The index numbers will remain the same, but the value will become empty. This array now lists as...

oranges
tomatoes

potatoes
bananas
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow