Creating Arrays in PHP
Creating an array in PHP is a simple task. An array() construct is used in PHP to create an array. Here are couple of examples with explanation for better understanding:
We first start with creating an Indexed or Numerical Array which is quite simple. Remember, all you have to do is use the array() construct. In this example, we will create an indexed array consisting of some protocols (our data elements). The position of first indexed element will be by default zero unless we change it.
<?php //Example 1 $protocols = array("TCP", "IP", "DNS", "FTP", "SMTP"); //TCP, IP, DNS, FTP and SMTP are data elements of the array ?>
Ok, we have got the array created but how do we know what position are the elements stored inside the array? How do we know what elements are actually stored in the array? It's simple. We will have to make use of print_r() construct in PHP which make our job easy. Here's how:
<?php //Example 2 $protocols = array("TCP", "IP", "DNS", "FTP", "SMTP"); print_r($protocols); //using the print_r() construct //Output: Array (  => TCP  => IP  => DNS  => FTP  => SMTP ) ?>
Look at the output of above code. At the first position is TCP and the first position is having an integer value of zero which is our key to this array. We used print_r() to print the elements of the array and their positions. If the data elememnts stored are very large in number consider not using the print_r() as it will fill up your screen with its output. Now let's take a look at another way of creating the same array in example #2.
<?php //Example 3 $protocols = "TCP"; $protocols = "IP"; $protocols = "DNS"; $protocols = "FTP"; $protocols = "SMTP"; print_r($protocols); //Output: Array (  => TCP  => IP  => DNS  => FTP  => SMTP ) ?>
Ok, we are now familiar with both the ways of creating an indexed array, now let's take a look at creating an Associative array. Remember, in associative array, the key value used for storing the position of an elements inside the array is a string but not an integer.
<? //Example 4 $protocol['TCP'] = "Transmission Control Protocol"; $protocol['DNS'] = "Dynamic Naming System"; $protocol['IP'] = "Internet Protocol"; $protocol['FTP'] = "File Transfer Protocol"; print_r($protocol); //Output: Array ( [TCP] => Transmission Control Protocol //[DNS] => Dynamic Naming System [IP] => Internet Protocol //[FTP] => File Transfer Protocol ) //Associate array creation can also be done using array() construct $months = array('Jan'=>"January", 'Feb'=>"February", 'Mar'=>"March"); print_r($months); //Output: Array ( [Jan] => January [Feb] => February [Mar] => March ) echo $months['Jan']; //Referring an individual element of the array //Output: January ?>
In an associative array we refer to position of a stored data element by the key of the index which is a string. But then, you can always have a string like "01", "02" as the key or index for elements of an array. It's just that "01" and "02" are considered as strings but if all the key values were mentioned as integers such as 1, 2 ,3 ,4 then the index will consist of all numerical values which will make it an indexed array.
There are certain requirements some times due to which you may not want an indexed array to have it's starting index number as zero instead you may want to begin your index at 1. Such is a requirement when you want to store all months in an array and we all know month numbers starts with 1 and not zero. Here is how we do it:
<? //Example 5 $months = array(1=>"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"); print_r($months); //Output: Array (  => Jan  => Feb  => Mar  => Apr  => May  => June //  => July  => Aug  => Sep  => Oct //  => Nov  => Dec ) ?>
In the above example we manually specified numerical value of the first position in the array. The remaining values were automatically incremented based on the first key value. You can mention any other number as the first key value and not necessarily it has to start with 0 or 1. Only in case of an indexed array where a starting key value is not specified, it's automatically started with a zero.
Now that we are familiar with creation of both indexed and associate arrays, let's look at some common and simple tasks related to arrays.
How to assign a range of values to an array?
To accomplish this task we will make use of the range() function, usage of which is fairly simple and straight. Check this example:
<? //Example 6 $alphabets = range('a','z'); // The above will hold all alphabets from a to z, key starts at 0 $numbers = range (1, 100); //All numbers from 1 to 100, key starts at 0 ?>
Determining the size of an array?
Often, you would come across some or the other requirement in your coding to first find the size of an array (number of elements) and then do some looping to retrive values or perform some other array related operation. Use count() or sizeof() functions for this purpose. It's simple:
<? //Example 7 $alphabets = range('a','z'); // The above will hold all alphabets from a to z, key starts at 0 echo count($alphabets); //Output: 26 $numbers = range (1, 100); //All numbers from 1 to 100, key starts at 0 echo count($numbers); //Output: 100 ?>
Let's take a look at some very basic and most commonly used array related tasks in next topic Array Operations in PHP.