About us Privacy Disclaimer Contact us
FAQ Help Advertising Feedback
Home Sitemap Search Donate us

  Home > Computer & I'net > Push and Pop...

   Browse by title articles:
   What is hot:

Accessing Arrays

Sorting Arrays

Push and Pop

Exploding and Imploding

Regular Expressions

Metacharacters

Steps To Optimizing Your HTML Codes
Keyboard Shortcuts for a "Frozen" Mouse
Character Classes
Bad Web Design: Looong Pages
Prev articles12345678 9 101112Next articles



Push and Pop


Computer & I'net articlesPush and Pop

by David Stanley    



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



-----------------
Article by David Stanley. Visit his site http://www.htmlite.com. Reprinted with permission.





Cost Spreading




  Disclaimer | Privacy | Terms of useCopyright © 2004 Nice2know.com