PHP
  Home arrow PHP arrow Page 4 - 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 - Single dimension arrays


    (Page 4 of 6 )

    As you should know by now, an array is simply a memory address, which is referenced by a variable name. Each indexes memory address is the same as its previous index, plus the size of the value stored in the current index. In PHP, you can declare an array using the array function, like in the following example:

    <?

    $Aarthur = array(

    0 => 'Get Value 01',

    1 => 'Get Value 02',

    2 => 'Get Value 03',

    3 => 'Get Value 04',

    4 => 'Get Value 05'

    );

    ?>


    This would create the variable $Arthur as an array containing five indexes from 0 to 4. The values of each index will also be filled in. The code below does exactly the same thing:

    <?

    $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";

    ?>


    The important thing to remember is that when we create our $Arthur variable using the array() function, we have to understand how they are accessed, and make sure we reference each index properly. For instance:

    <?

    $Arthur = array(

    0 => 'Get Value 01'

    );

    ?>


    This creates a new array variable named $Arthur. The value of 'Get Value 01' is pointed to by using the index of 0:

    print $Arthur[0];

    However, if we have the following code:

    <?

    $Arthur = array(

    '0' => 'Get Value 01'

    );

    ?>


    This would create the $Arthur array in the same way, however instead of accessing the index numerically, we would now access it using the index '0', which is a string. We would change '0' to any other string we like:

    <?

    $Arthur = array(

    'first_index' => 'Get Value 01'

    );

    ?>


    There is one pitfall to avoid, however. When we're creating a new array variable, let's say that we used the following declaration:

    <?

    $Arthur = array(add => "Get Value Add");

    ?>


    Although there is nothing wrong with this declaration, it is not syntactically correct. We are trying to create a new array with one index named add. Because add is not defined anywhere, the PHP parsing engine may raise a warning error to alert you of this. The proper way to create an array like this would be:

    <?

    $Arthur = array("add" => "Get Value Add");

    ?>


    Notice now that we have a single-indexed array with one index named "add". We can now refer to our $Arthur array like this:

    print $Arthur["add"]; // Would print "Get Value Add"

    This allows us to create arrays with indexes that can either be referenced numerically or by strings. We can go crazy and combine both numerical and string indexes, like this:

    <?

    $Arthur = array("first" => "Get Value Add", 1 => "Value One", "Two" => 2);

    Print $Arthur["first"] . "<br>";

    Print $Arthur[1] . "<br>";

    Print $Arthur["Two"] . "<br>";

    ?>


    In the browser, our output would look like this:

    Using both numerical and string indexes in one declaration

    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