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  
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? 
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 - 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

    - 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
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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