PHP
  Home arrow PHP arrow Page 2 - 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? 
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 - Having Your Cake
    ( Page 2 of 8 )

    As you may have already guessed, defining an array variable takes place via the array() function - here's how:

    <? $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake"); ?>
    The rules for choosing an array variable name are the same as those for any other PHP variable - it must begin with a letter, and can optionally be followed by more letters and numbers.

    Alternatively, you can define an array by specifying values for each element in the index notation, like this:

    <? $desserts[0] = "chocolate mousse"; $desserts[1] = "tiramisu"; $desserts[2] = "apple pie"; $desserts[3] = "chocolate fudge cake"; ?>
    Incidentally, you can omit the index numbers if you prefer - the following snippet is equivalent to the one above:

    <? $desserts[] = "chocolate mousse"; $desserts[] = "tiramisu"; $desserts[] = "apple pie"; $desserts[] = "chocolate fudge cake"; ?>
    If you'd prefer to use keys instead of numeric indices, the following examples should make you happier:

    <? $movies = array("romance" => "Moulin Rouge", "epic" => "Gladiator", "action" => "The Terminator"); // this is equivalent $movies["romance"] = "Moulin Rouge"; $movies["epic"] = "Gladiator"; $movies["action"] = "The Terminator"; ?>
    You can use the range() function to automatically create an array containing a range of elements:

    <? // returns the array ("30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40") $thirties = range(30, 40); // returns the array ("i", "j", "k", "l", "m", "n", "o") $alphabet = range("i", "o"); ?>
    You can add elements to the array in a similar manner - for example, if you wanted to add the element "apricot fritters" to the $desserts array, you would use this:

    <? $desserts[4] = "apricot fritters"; ?>
    and the array would now look like this:

    <? $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters"); ?>
    The same goes for non-numeric keys - adding a new key to the $movies array

    <? $movies["horror"] = "The Sixth Sense"; ?>
    alters it to read:

    <? $movies = array("romance" => "Moulin Rouge", "epic" => "Gladiator", "action" => "The Terminator", "horror" => "The Sixth Sense"); ?>
    In order to modify an element of an array, you would simply assign a new value to the corresponding scalar variable. If you wanted to replace "chocolate fudge cake" with "chocolate chip cookies", you'd simply use

    <? $desserts[3] = "chocolate chip cookies"; ?>
    and the array would now read

    <? $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate chip cookies", "apricot fritters"); ?>
    You can do the same with named keys - the statement

    <? $movies["action"] = "Rambo"; ?>
    further alters the $movies array to

    <? $movies = array("romance" => "Moulin Rouge", "epic" => "Gladiator", "action" => "Rambo", "horror" => "The Sixth Sense"); ?>
    If it walks like an array, talks like an array and looks like an array, it must be an array...right? Well, you can always verify your suspicions with the is_array() function, which comes in handy if you need to test whether a particular variable is an array or not.

    <? // create array $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters"); // returns true echo is_array($desserts); $desserts = "blueberry ice cream"; // returns false echo is_array($desserts); ?>


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

       

    PHP ARTICLES

    - 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 ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT