PHP
  Home arrow PHP arrow Page 6 - 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 
Moblin 
JMSL Numerical Library 
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 - Where Am I?


    (Page 6 of 8 )

    A number of built-in functions are available for use while iterating through an array - you'll typically use these in combination with a "while" or "for" loop.

    The current() function returns the currently-in-use element of an array (beginning with the first element),

    <? // create array $friends = array("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross"); // returns "Rachel" echo current($friends); ?>
    while the key() function returns the corresponding array key.

    <? // create array $friends = array("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross"); // returns "Rachel" echo current($friends); // returns 0 echo key($friends); ?>
    The next() and prev() functions move forward and backward through the array.

    <? // create array $friends = array("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross"); // returns "Rachel" echo current($friends); // move forward next($friends); // returns "Monica" echo current($friends); // move forward next($friends); // returns "Phoebe" echo current($friends); // move backwards prev($friends); // returns "Monica" echo current($friends); ?>
    The reset() and end() functions move to the beginning and end of an array respectively,

    <? // create array $friends = array("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross"); // returns "Rachel" echo current($friends); // move to end end($friends); // returns "Ross" echo current($friends); // move forward reset($friends); // returns "Rachel" echo current($friends); ?>
    while the each() function comes in handy when you need to iterate through an array.

    <? // create array $music = array("pop", "rock", "jazz", "blues"); // creates the array ("0" => 0, "1" => "pop", "key" => 0, "value" => "pop") $this = each($music); // returns "0" - no key, as this is not a hash echo $this["0"]; // returns "pop" echo $this["1"]; // returns "0" - no key, as this is not a hash echo $this["key"]; // returns "pop" echo $this["value"]; ?>
    Confused? Don't be - every time each() runs on an array, it creates a hash containing four keys: "0", "1", "key" and "value". The "0" and "key" keys contain the name of the currently-in-use key of the array (or hold the value 0 if the array does not contain keys), while the "1" and "value" keys contain the corresponding value.

    Here's another example to make this clearer:

    <? // create array $music = array("pop" => "Britney Spears", "rock" => "Aerosmith", "jazz" => "Louis Armstrong"); // creates the array ("0" => "pop", "1" => "Britney Spears", "key" => "pop", "value" => "Britney Spears") $this = each($music); // returns "pop" echo $this["0"]; // returns "Britney Spears" echo $this["1"]; // returns "pop" echo $this["key"]; // returns "Britney Spears" echo $this["value"]; // move forward // creates the array ("0" => "rock", "1" => "Aerosmith", "key" => "rock", "value" => "Aerosmith") $this = each($music); ?>

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


     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





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