PHP
  Home arrow PHP arrow 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Array Manipulation With PHP4


    (Page 1 of 8 )

    As a PHP developer myself, I spend a fair part of my time looking for ways to write cleaner, more efficient code. I'll be the first to admit that I don't succeed too often; however, that doesn't stop me from making the effort. And nine times out of ten, I don't have to look too far - the PHP community is incredibly generous with its advice and assistance, and the language itself offers tools which can slice through complex problems in much the same way as a hot knife slices through butter.

    Nowhere is this clearer than in the case of PHP's built-in functions, which offer developers power and flexibility of the sort not seen in most other languages. Sadly, most developers don't use this power - not because they don't want to, but simply because they're not even aware of the full extent of PHP's capabilities. And this lack of knowledge can often spell the difference between a tight, efficient Web application and a slow, inefficient one.

    Over the next few pages, I'm going to illustrate my point by taking an in-depth look at PHP's array manipulation capabilities. If you're like most developers, you probably use arrays extensively in your development activities; however, your knowledge of array manipulation techniques is limited to counting the elements of an array or iterating through key-value pairs.

    PHP's array manipulation functions allow you to do much, much more...and this article will open your eyes to the possibilities.

    In addition to providing a gentle introduction to PHP programming in general (and PHP array manipulation in particular), this article will offer you a broad overview of PHP's array functions, serving as both a handy reference and a tool to help you write more efficient code. Regardless of whether you're new to PHP or if you've been working with the language for a while, you should find something interesting in here.

    Let's get started!{mospagebreak title=Just Friends} We'll begin right at the top, with some very basic definitions and concepts.

    Unlike string and numeric variables, which typically hold a single value, an array can best be thought of as a "container" variable, which can contain one or more values. For example,

    <? $friends = array("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross"); ?>
    Here, $friends is an array variable, which contains the values "Rachel", "Monica", "Phoebe", "Joey", "Chandler", and "Ross".

    Array variables are particularly useful for grouping related values together - names, dates, phone numbers of ex-girlfriends et al.

    The various elements of the array are accessed via an index number, with the first element starting at zero. So, to access the element

    "Rachel"
    I would use the notation

    $friends[0]
    while

    "Joey"
    would be

    $friends[3]
    - essentially, the array variable name followed by the index number enclosed within square braces. Geeks refer to this as "zero-based indexing".

    PHP also allows you to replace indices with user-defined "keys", in order to create a slightly different type of array. Each key is unique, and corresponds to a single value within the array.

    <? $starwars = array("new hope" => "Luke", "teacher" => "Yoda", "bad guy" => "Darth"); ?>
    In this case, $starwars is an array variable containing three key-value pairs. The => symbol is used to indicate the association between a key and its value.

    In order to access the value

    "Luke"
    I would use the key "new hope"

    $starwars["new hope"]
    while the value

    "Darth"
    would be accessible via the key "bad guy"

    $starwars["bad guy"]
    This type of array is sometimes referred to as a "hash" or "hash table" - if you've ever used Perl, you'll see the similarities to the Perl hash variable.

    PHP arrays can store strings, number and other arrays. In fact, the same array can store multiple types of data, a luxury not available in many other programming languages. Here's an example:

    <? // create array $all_mixed_up = array( "numbers" => array("one", 2, 3, "four"), "superheroes" => array("spiderman", "superman", "captain marvel"), "icecream" => array("chocolate" => "brown", "vanilla" => "white", "strawberry" => "pink"), ); // returns null - no element with key 2 echo $all_mixed_up[2]; // returns "Array" echo $all_mixed_up["numbers"]; // returns "one" echo $all_mixed_up["numbers"][0]; // returns "captain marvel" echo $all_mixed_up["superheroes"][2]; // returns "white" echo $all_mixed_up["icecream"]["vanilla"]; ?>

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


     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway