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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 - 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

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





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