PHP
  Home arrow PHP arrow Page 3 - Arrays
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Arrays
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 36
    2005-06-23

    Table of Contents:
  • Arrays
  • Outputting Arrays
  • Testing for an array
  • Locating Array Elements
  • Determining Array Size and Uniqueness
  • Other Useful Array Functions

  • 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


    Arrays - Testing for an array


    (Page 3 of 6 )

    When you incorporate arrays into your application, you’ll sometimes need to know whether a particular variable is an array. A built-in function, is_array(), is available for accomplishing this task.

    is_array()

    boolean is_array(mixed variable)

    The is_array() function determines whether variable is an array, returning TRUE if it is, and FALSE otherwise. Note that even an array consisting of a single value will still be considered an array. An example follows:

    $states = array("Florida");
    $state = "Ohio";
    echo "\$states is an array: ".is_array($states)."<br />"
    ;
    echo "\$state is an array: ".is_array($state)."<br />";

    The results are:

    ====================================================
    $states is an array: 1
    $state is an array: ====================================================

    Adding and Removing Array Elements

    PHP provides a number of functions for both growing and shrinking an array. Some of these functions are provided as a convenience to programmers who wish to mimic various queue implementations (FIFO, LIFO, and so on), as reflected by their names (push, pop, shift, and unshift). Even if you don’t know what queue implementations are, don’t worry; these functions are easy to use, and examples are provided for each.

    NOTE A traditional queue is a data structure in which the elements are removed in the same order that they were entered, known as first-in-first-out, or FIFO. In contrast, a stack is a data structure in which the elements are removed in the order opposite to that in which they were entered, known as last-in-first-out, or LIFO.

    $arrayname[]

    This isn’t a function, but a language feature. You can add array elements simply by executing the assignment, like so:

    $states["Ohio"] = "March 1, 1803";

    In the case of a numerical index, you can append a new element like this:

    $state[] = "Ohio";

    Sometimes, however, you’ll require a somewhat more sophisticated means for adding array elements (and subtracting array elements, a feature not readily available in the fashion described for adding elements). These functions are introduced throughout the remainder of this section.

    array_push()

    int array_push(array target_array, mixed variable [, mixed variable...])

    The array_push() function adds variable onto the end of the target_array, returning TRUE on success and FALSE otherwise. You can push multiple variables onto the array simultaneously, by passing these variables into the function as input parameters. An example follows:

    $states = array("Ohio","New York");
    array_push($states,"California","Texas");
    // $states = array("Ohio","New York","California","Texas");

    array_pop()

    mixed array_pop(array target_array)

    The array_pop() function returns the last element from target_array, resetting the array pointer upon completion. An example follows:

    $states = array("Ohio","New York","California","Texas"); $state = array_pop($states); // $state = "Texas"

    More PHP Articles
    More By Apress Publishing


       · The writer forgot something important. Not only does array_pop return the last...
       · The capital of Nebraska, used in the example, is Lincoln (not Omaha).
     

    Buy this book now. This article is excerpted from the book Beginning PHP 5 and MySQL: From Novice to Professional, by W. Jason Gilmore (Apress, 2004; ISBN: 1893115518). Check it out at your favorite bookstore today. Buy this book now.

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





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