PHP
  Home arrow PHP arrow Page 4 - Array Manipulation With PHP4
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Array Manipulation With PHP4
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2001-11-09

    Table of Contents:
  • Array Manipulation With PHP4
  • Having Your Cake
  • When Size Does Matter...
  • Push And Pull
  • Slice And Dice
  • Where Am I?
  • Sorting Things Out
  • Flipping Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Array Manipulation With PHP4 - Push And Pull


    (Page 4 of 8 )

    You can add an element to the end of an existing array with the array_push() function,

    <? // create array $superheroes = array("spiderman", "superman", "captain marvel", "green lantern"); array_push($superheroes, "the incredible hulk"); // $superheroes now contains ("spiderman", "superman", "captain marvel", "green lantern", "the incredible hulk") ?>
    and remove an element from the end with the interestingly-named array_pop() function.

    <? // create array $superheroes = array("spiderman", "superman", "captain marvel", "green lantern"); array_pop($superheroes); // $superheroes now contains ("spiderman", "superman", "captain marvel") ?>
    If you need to pop an element off the top of the array, you can use the array_shift() function,

    <? // create array $superheroes = array("spiderman", "superman", "captain marvel", "green lantern"); array_shift($superheroes); // $superheroes now contains ("superman", "captain marvel", "green lantern") ?>
    while the array_unshift() function takes care of adding elements to the beginning of the array.

    <? // create array $superheroes = array("spiderman", "superman", "captain marvel", "green lantern"); array_unshift($superheroes, "the human torch"); // $superheroes now contains ("the human torch", "spiderman", "superman", "captain marvel", "green lantern") ?>
    In case you need an array of a specific length, you can use the array_pad() function to create an array and pad it with empty elements (or elements containing a user-defined value). The following example should make this clearer:

    <? // create array $students = array(); // returns an array containing 4 null values $students = array_pad($students, 4, ""); ?>
    The second argument also specifies the direction of padding, while the third argument to the function specifies the value to be used for the empty elements.

    <? // create array $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters"); // returns an array containing 8 elements // ("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters", "dummy", "dummy", "dummy") // array is padded to the right since the size is positive $desserts = array_pad($desserts, 8, "dummy"); // this would return the same array, but padded to the left (note the negative size) // ("dummy", "dummy", "dummy", "chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters") $desserts = array_pad($desserts, -8, "dummy"); ?>

    More PHP Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway