Difference between revisions of "PHP Arrays"

From Techotopia
Jump to: navigation, search
(Accessing Elements in a PHP Array)
Line 30: Line 30:
  
 
The elements in a PHP array using a numberical key are accessed by referencing the variable contain ing the array, followed by the index into array of the required element enclose din square brackets ([]). We can extend our previous example to display the value contained in the second element of the array (remmeber that the index is zero based so the first element is element 0:
 
The elements in a PHP array using a numberical key are accessed by referencing the variable contain ing the array, followed by the index into array of the required element enclose din square brackets ([]). We can extend our previous example to display the value contained in the second element of the array (remmeber that the index is zero based so the first element is element 0:
 +
 +
<pre>
 +
<?php
 +
      $colorArray = array("Red", "Yellow", "Green", "Blue", "Indigo");
 +
   
 +
      echo $colorArray[1];
 +
?>
 +
</pre>
 +
 +
The above ''echo'' command will output the value in index postion 1 of the array, in this case the string "Yellow".
 +
 +
== Creating an Associative Array ==
 +
 +
As described earlier an associative array assigns names to postions in the array. This provides a more ''human friendly'' way to access arrasy elements that the numerical key approach outlined above. Once again the ''array()'' function is used to create the array, with option arguments to pre-initialize the array elements. In the case of associative arrasy the arguments are of the form ''key=>value'' where key is the name by which the elemt will be referenced, and value is the value to be stored in taht position in the array.
 +
 +
Suppose we are creating an array to store bank customer information. We can assign a key name to each elelemt in the array as follows:
 +
 +
<pre>
 +
<?php
 +
$customerArray = array('customerName'=>'John Smith', 'customerAddress'=>'1 The Street', 'accountNumber'=>'123456789');
 +
?>
 +
</pre>
 +
 +
== Accessing Elements of an Associative Array ==
 +
 +
Now that we have created an associative array and assigned names to each element we can use those names to access the corresponding array values. We can, therefore extend our previous example to extract the customer name from our ''$customerArray'':
 +
 +
<pre>
 +
<?php
 +
      $customerArray = array('customerName'=>'John Smith', 'customerAddress'=>'1 The Street', 'accountNumber'=>'123456789');
 +
 +
      echo $customerArray['customerName'];
 +
?>
 +
 +
The result will be the customer name "John Smith" appearing in the browser window.

Revision as of 17:08, 1 June 2007

PHP Arrays provide a way to group together many variables such that they can be referenced and manipulated using a single variable. An array is, in many ways, a self-contained list of variables.

Once an array has been created items can be added, removed and modified, sorted and much more. The items in an arrasy can be of any variable type and an array can contain any mixture of data types - it is not necessary to have each element in the array of the same type.

Elements of an array are accessed using a key. There are two types of array depending on the type of key that is used to access an array element. In a numerical key array elements are accessed by specifying the numerical position of the item in the array. The first item in an array is element 0, the second is element 1 and so on. The second type of array is the associative array where a name is given to each element and that name is used to access the corresponding array element.


Contents


Create a PHP Array

Arrays are created using the array() function. The array() function takes zero or more arguments and returns the new array which must be assigned to a variable using the assigment operator (=). If arguments are provided they are used to initialize the array with data.

PHP arrays grow and shrink dynamically as items are added and removed so it is not necessary to specify the array size at creation time as it is with some other programming languages.

We can create an empty array as follows:

<?php
      $colorArray = array();
?>

Alternatively, we can create an array pre-initialized with values by providing the values as arguments to the array() function:

<?php
      $colorArray = array("Red", "Yellow", "Green", "Blue", "Indigo");
?>

Accessing Elements in a PHP Array

The elements in a PHP array using a numberical key are accessed by referencing the variable contain ing the array, followed by the index into array of the required element enclose din square brackets ([]). We can extend our previous example to display the value contained in the second element of the array (remmeber that the index is zero based so the first element is element 0:

<?php
      $colorArray = array("Red", "Yellow", "Green", "Blue", "Indigo");
    
      echo $colorArray[1];
?>

The above echo command will output the value in index postion 1 of the array, in this case the string "Yellow".


Creating an Associative Array

As described earlier an associative array assigns names to postions in the array. This provides a more human friendly way to access arrasy elements that the numerical key approach outlined above. Once again the array() function is used to create the array, with option arguments to pre-initialize the array elements. In the case of associative arrasy the arguments are of the form key=>value where key is the name by which the elemt will be referenced, and value is the value to be stored in taht position in the array.

Suppose we are creating an array to store bank customer information. We can assign a key name to each elelemt in the array as follows:

<?php
$customerArray = array('customerName'=>'John Smith', 'customerAddress'=>'1 The Street', 'accountNumber'=>'123456789');
?>

Accessing Elements of an Associative Array

Now that we have created an associative array and assigned names to each element we can use those names to access the corresponding array values. We can, therefore extend our previous example to extract the customer name from our $customerArray:

<?php
      $customerArray = array('customerName'=>'John Smith', 'customerAddress'=>'1 The Street', 'accountNumber'=>'123456789');

      echo $customerArray['customerName'];
?>

The result will be the customer name "John Smith" appearing in the browser window.