PHP
  Home arrow PHP arrow Page 3 - Arrays and PHP: A Primer
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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

Arrays and PHP: A Primer
By: Tuna Celik
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 24
    2001-12-20

    Table of Contents:
  • Arrays and PHP: A Primer
  • The fundamentals of memory allocation
  • Arrays and their uses
  • Single dimension arrays
  • Multi-dimensional arrays
  • Conclusion

  • 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


    Arrays and PHP: A Primer - Arrays and their uses


    (Page 3 of 6 )

    Before we continue, you should understand that an array is simply a value stored in memory with more than one segment of allocation, meaning that one variable can "store" more than one value. Each value is indexed either numerically, or by a text reference. Here's an example:

    <?

    $Arthur[0] = "Value01";

    $Arthur[1] = "Value02";

    $Arthur[2] = "Value03";

    $Arthur[3] = "Value04";

    $Arthur[4] = "Value05";

    ?>


    As you can see from the example above, the $Arthur variable is now an array, and can take five values, which are indexed from 0 to 4. Here's an idea of how the $Arthur variable would look in memory:

    The $Arthur array in memory

    The address of the $Arthur variable is the address of the first index, 0. The address of each index after that is calculated as the address of the previous index, plus the size of the data type being stored. For example, if $Arthur[0] has the address of 0x19DA00 and the $Arthur array was storing only integers (4 bytes), then the address of $Arthur[1] would be 0x19DA04.

    To print out the values stored in the $Arthur array, we would use this snippet of code:

    <?

    $arthur[0] = "Get Value 01";

    $arthur[1] = "Get Value 02";

    $arthur[2] = "Get Value 03";

    $arthur[3] = "Get Value 04";

    $arthur[4] = "Get Value 05";

    for($i = 0; $i < 5; $i++) {

    print $arthur[$i] . "<br>";

    }

    ?>


    The print command will output each of the values to our browser, as shown below:

    The results of printing each index in the $Arthur array

    There are many different ways that we can declare both single and multi-dimensional arrays in PHP. Let's look at some of them now.

    More PHP Articles
    More By Tuna Celik


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







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