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  
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? 
Google.com  
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
    ( Page 1 of 8 )

    This may come as a bit of a shock to you, but PHP's array functions can do a lot more than just count the elements of an array or iterate through key-value pairs. This article takes an in-depth look at PHP's less well-known array manipulation tools, illustrating how they can be used to write tighter, more efficient code...and have some fun in the bargain as well!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

    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek