PHP
  Home arrow PHP arrow Page 3 - 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 - When Size Does Matter...
    ( Page 3 of 8 )

    Once an array has been created, it's time to use it. Within the context of an array, the most important (and commonly-used) function is the sizeof() function, which returns the size of (read: number of elements within) the array.

    Here's an example:

    <? // create array $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters"); // returns 5 echo sizeof($desserts); // create array $movies = array("romance" => "Moulin Rouge", "epic" => "Gladiator", "action" => "Rambo", "horror" => "The Sixth Sense"); // returns 4 echo sizeof($movies); ?>
    If you're using a hash, the array_keys() and array_values() functions come in handy to get a list of all the keys and values within the array.

    <? // create array $starwars = array("princess" => "Leia", "teacher" => "Yoda", "new hope" => "Luke", "bad guy" => "Darth", "worse guy" => "The Emperor"); // returns the array ("princess", "teacher", "new hope", "bad guy", "worse guy") array_keys($starwars); // returns the array ("Leia", "Yoda", "Luke", "Darth", "The Emperor") array_values($starwars); ?>
    And the in_array() function can tell you whether or not a particular value exists in an array.

    <? // create array $starwars = array("princess" => "Leia", "teacher" => "Yoda", "new hope" => "Luke", "bad guy" => "Darth", "worse guy" => "The Emperor"); // returns true echo in_array("Yoda", $starwars); // returns false echo in_array("Obi-Wan", $starwars); ?>
    An alternative (and sometimes more efficient) approach to the one above would be to scan the array for a value match and return the corresponding key. If this is what you need, consider the array_search() function, which is designed just for this purpose:

    <? // create array $starwars = array("princess" => "Leia", "teacher" => "Yoda", "new hope" => "Luke", "bad guy" => "Darth", "worse guy" => "The Emperor"); // returns "bad guy" echo array_search("Darth", $starwars); ?>
    You can convert array elements into regular PHP variables with the list() and extract() functions. The list() function assigns array elements to variables,

    <? // create array $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters"); // assign elements to variables list($a, $b, $c, $d, $e) = $desserts; // returns "tiramisu" echo $b; ?>
    while the extract() function iterates through a hash, converting the key-value pairs into corresponding variable-value pairs.

    <? // create array $starwars = array("princess" => "Leia", "teacher" => "Yoda"); // assign elements to variables extract($starwars); // returns "Leia" echo $princess; ?>


     
     
    >>> 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 4 Hosted by Hostway
    Stay green...Green IT