You are on page 1of 2

Being a programmer you will be tackling large heaps of data many a times, in php also, like storing the

username, email, password and other sensitive data of large no. of users. Such kind of data sets also need to be manipulated, sorted and retrieved. Luckily php as many other programming languages, also provides you with the feature of ARRAY which provides you with the ideal way to store, manipulate, sort and retrieve data sets. You are already familiar with the array data type. In this series of lectures, arrays will be covered from basics of creating an array to merging and slicing an array. After this series of lectures, specifically, you will be able to do these things namely: 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 rushing to these topics, let me first introduce you to our not so new data type- 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 values position in the array(Like many other programming languages, PHPs numerically indexed arrays begins with position 0, and not 1). 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 the following: $states = array(0 => "Alabama", 1 => "Alaska"...49 => "Wyoming"); // $states[0] = Alabama Whereas an associative key logically bears a direct relation to its corresponding value. Mapping arrays associatively is particularly convenient when using numerical index values just doesnt 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("HR" => "Haryana", "HP" => "Himachal Pradesh", "NY" => "New York") You could then reference Haryana like this: $states["HR"]

Its also possible to create arrays of arrays, known as multidimensional arrays. For example, you could use a multidimensional array to store the state information. Using PHP syntax, it might look like this: $states = array ( "Haryana" => array("population" => "11,353,140", "capital" => "Chandigarh"), "Punjab" => array("population" => "1,711,263", "capital" => "Chandigarh") // capital of Haryana and Punjab is same ); You could then reference Haryanas population: $states["Haryana"]["population"] This would return the following: 11,353,140 Note: For the above example, Haryana and Punjab are the keys in the array $states whereas population and capital are the keys in both arrays Haryana and Punjab.

You might also like