| Home > Computer & I'net > Push and Pop... |
|
|
|||||||||||||||
|
|
|||||||||||||||
|
||||||||||||||||
Push and Pop | |
<?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. | |
| Articles |
•Auto & Trucks•Business•Computer & I'net·General·Apache·CSS·Database·Hardware·HTML·Javascript&DHTML·Linux·MySQL·Operating System·Perl / CGIPHP·Programming·Publishing·Search Engines·Software Problems·SSI·Tips & Tricks·Utilities·Web Design•Family•Food & Drink•Gardening•Health•Other•Pets•Psychology•Spiritual•Travel•Women |
| Calculators |
|