Posted on Updated on

Image result for ARRAYARRAY

Much of your time as a programmer is spent working with data sets. Some examples of data sets include the names of all employees at a corporation; the U.S. presidents and their corresponding birth dates; and the years between 1900 and 1975. In fact, working with data sets is so prevalent that a means for managing these groups within code is a common feature of all mainstream programming languages. Within the PHP language, this feature is known as an array, and it offers an ideal way to store, manipulate, sort, and retrieve data sets. PHP’s array support and the language’s impressive variety of functions used to work with them. Specifically, you’ll learn how to do the following:

  • Create arrays
  • Output arrays
  • Test for an array
  • Add and remove array elements
  • Locate array elements
  • Traverse arrays
  • Determine array size and element uniqueness
  • Sort arrays
  • Merge, slice, splice, and dissect arrays

Before beginning the overview of these functions, let’s take a moment to formally define an array and review some fundamental concepts on how PHP regards this important data type.

What Is an Array?

An array is traditionally defined as a group of items that share certain characteristics, such as similarity (car models, baseball teams, types of fruit, etc.) and type (e.g., all strings or integers). Each item is Distinguished by a special identifier known as a key. PHP takes this definition a step further, forgoing the requirement that the items share the same data type. For example, an array could quite possibly contain items such as state names, ZIP codes, exam scores, or playing card suits.

Each item consists of two components: the aforementioned key and a value. The key serves as the lookup facility for retrieving its counterpart, the value. Keys can be numerical or associative. Numerical keys bear no real relation to the value other than the value’s position in the array. As an example, thearray 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 the following:

$states = array(0 => “Alabama”, 1 => “Alaska”…49 => “Wyoming”);

Using numerical indexing, you could reference the first state in the array (Alabama) like so:


■ Note Like many programming languages, PHP’s numerically indexed arrays begin with position 0, not 1.

An associative key logically bears a direct relation to its corresponding value. Mapping arrays associatively is particularly convenient when using numerical index values just doesn’t make sense. For instance, you might want to create an array that maps state abbreviations to their names. Using PHP syntax, this might look like the following:

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

You could then reference Ohio like this:


It’s also possible to create arrays of arrays, known as multidimensional 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:


This would return the following:


Logically, you’ll require a means for traversing arrays. As you’ll learn throughout this chapter, PHP offers many ways to do so. Regardless of whether you’re using associative or numerical keys, 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 using either built-in language features or functions. Still, it’s useful to understand this basic concept.

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666


Please fill the FORM and we shall contact you soon.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s