Home arrow PHP arrow Page 3 - Arrays

Testing for an array - PHP

Arrays offer an ideal means for storing, manipulating, sorting, and retrieving the kinds of data sets that programmers spend a lot of time working with. PHP supports the array data type. This article explains the array-based features and functions it supports. It is excerpted from the book, Beginning PHP 5 and MySQL: From Novice to Professional, by W. Jason Gilmore (Apress, 2004; ISBN: 1893115518).

TABLE OF CONTENTS:
  1. Arrays
  2. Outputting Arrays
  3. Testing for an array
  4. Locating Array Elements
  5. Determining Array Size and Uniqueness
  6. Other Useful Array Functions
By: Apress Publishing
Rating: starstarstarstarstar / 37
June 23, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: