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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Array Manipulation With PHP4
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek