Home arrow PHP arrow Arrays


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).

  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



PROGRAMMERS SPEND A considerable amount of time working with sets of related data. The names of all employees at the XYZ corporation, U.S. Presidents and their corresponding birth dates, and years between 1900 and 1975 are all examples of related data sets. Because working with such groups of data is so prevalent, it might not come as a surprise that a means for managing these groups within code is a common feature across all mainstream programming languages. This means typically centers around the compound data type array, which offers an ideal means for storing, manipulating, sorting, and retrieving data sets. PHP’s solution is no different, supporting the array data type, in addition to an accompanying host of behaviors and functions directed towards array manipulation. In this chapter, you’ll learn all about those array-based features and functions supported by PHP. Although it is beyond the scope of this book to provide a comprehensive list of all predefined functions, you’ll nonetheless find information regarding the majority of array functions in this chapter, because PHP’s array-handling capabilities are just so darned useful.

Because I introduce so many functions in this chapter, I thought it wise to divide them into the following categories:

  • Outputting arrays

  • Creating arrays

  • Testing for an array

  • Adding and removing array elements

  • Locating array elements

  • Traversing arrays

  • Determining array size and element uniqueness

  • Sorting arrays

  • Merging, slicing, splicing, and dissecting arrays

Rather than simply list them alphabetically, the categorical division should aid in later reference when searching for a viable solution to some future problem. Before beginning this review, I’ll start the chapter with a brief recap of the array definition and structure.

What Is an Array?

An array is traditionally defined as a group of items that share the same characteristics and are distinguished by their lookup key (more about this key in a bit). I use the word traditionally because you can flaunt this definition and group entirely unrelated entities together in an array structure. PHP takes this a step further, foregoing the requirement that they even share the same datatype. For example, an array might contain items like state names, zip codes, exam scores, or playing card suits. Each entity consists of two items: a key and a value. The key serves as the lookup facility for retrieving its counterpart, the value. These keys can be numerical, and bear no real relation to the value other than the value’s position in the array. As an example, the array could consist of an alphabetically sorted list of state names, with key 0 representing “Alabama”, and key 49 representing “Wyoming”. Using PHP syntax, this might look like this:

$states = array (0 => "Alabama", "1" => "Alaska"..."49" => "Wyoming");

Using numerical indexing, you could reference the first state like so:


NOTE  PHP’s numerically indexed arrays begin with position 0, not 1.

Alternatively, key mappings can be associative, where the key bears some relation to the value other than its array position. Mapping arrays associatively is particularly convenient when using numerical index values just doesn’t make sense. For example, I might want to create an array that maps state abbreviations to their names, like this: OH/Ohio, PA/Pennsylvania, and NY/New York. Using PHP syntax, this might look like:

$states = array ("OH" => "Ohio", "PA" => "Pennsylvania", "NY" => "New York")

You could then reference “Ohio” like so:


Arrays consisting solely of atomic entities are referred to as being single-dimensional. Multidimensional arrays consist of other arrays. For example, you could use a multidimensional array to store U.S. state information. Using PHP syntax, it might look like this:

$states = array (
  "Ohio" => array ("population" => "11,353,140", "capital"
  => "Columbus"),
  "Nebraska" => array("population" => "1,711,263",
  "capital" => "Omaha")

You could then reference Ohio’s population like so:


This would return the following value:


In addition to offering a means for creating and populating an array, the language must also offer a means for traversing it. As you’ll learn throughout this chapter, PHP offers many ways for doing so. Regardless of which way you use, keep in mind that all rely on the use of a central feature known as an array pointer. The array pointer acts like a bookmark, telling you the position of the array that you’re presently examining. You won’t work with the array pointer directly, but instead will traverse the array either using built-in language features or functions. Still, it’s useful to understand this basic concept.

>>> More PHP Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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