Arrays
Arrays are sets of data which can be defined in a PHP Script. Arrays can contain other arrays inside of them without any problems (called a multidimensional array). Arrays can be referred to as tables, even temporary databases. Making an array is very simple, using the array function.
Syntax
Arrays can be created in two ways, using function array or defining with a name and square brackets:
$my_array['key']='value';
Use of the array function is done differently:
$my_array=array(<key_name>=><value>);
If key_name is not defined, such as:
$my_array=array(<value>);
a new key will be created, with a number as a key number and value as value. This happens because every key needs to be defined. If it is not, it is unaccessible (which can't happen). In this example, key_name will be "0" because the array is empty.
Examples of Arrays
Example #1
<?php
$array = array("name"=>"Toyota","type"=>"Celica","colour"=>"black","manufactured"=>"1991");
$array2 = array("Toyota","Celica","black","1991");
$array3 = array("name"=>"Toyota","Celica","colour"=>"black","1991");
print_r($array);
print_r($array2);
print_r($array3);
?>
This Example will output this:
Array
(
[name] => Toyota
[type] => Celica
[colour] => black
[manufactured] => 1991
)
Array
(
[0] => Toyota
[1] => Celica
[2] => black
[3] => 1991
)
Array
(
[name] => Toyota
[0] => Celica
[colour] => black
[1] => 1991
)
Example #2
The following example will output the identical text as #Example #1:
<?php
$array['name']="Toyota";
$array['type']="Celica";
$array['colour']="black";
$array['manufactured']="1991";
$array2[]="Toyota";
$array2[]="Celica";
$array2[]="black";
$array2[]="1991";
$array3['name']="Toyota";
$array3[]="Celica";
$array3['colour']="black";
$array3[]="1991";
print_r($array);
print_r($array2);
print_r($array3);
?>
Example #3
Using the Example #1 and Example #2 above, now you can try and use arrays the same way as normal variables:
echo "Manufacturer: <b>{$array['name']}</b><br />\n";
echo "Brand: <b>{$array2['1']}</b><br />\n";
echo "Colour: <b>".$array3['colour']."</b><br />\n";
echo "Year Manufactured: <b>".$array3[1]."</b><br />\n"
which will output this:
Manufacturer: <b>Toyota</b><br />
Brand: <b>Celica</b><br />
Colour: <b>black</b><br />
Year Manufactured: <b>1991</b><br />
Note to contributors: This is an example of what the script outputs, not an example of how you would see the actual page. So don't edit and change to what the user actually sees (a rendered HTML page, not XHTML because the <b> element does not exist in XHTML)!
Multidimensional Arrays
Elements in an array can also be an array, allowing for multidimensional arrays. An example, keeping with the motoring examples above, is:
<?
$cars = array(
"car1" => array("make" => "Toyota","colour" => "Green","year" => 1999,"engine_cc" => 1998),
"car2" => array("make" => "BMW","colour" => "RED","year" => 2005,"engine_cc" => 2400),
"car3" => array("make" => "Renault","colour" => "White","year" => 1993,"engine_cc" => 1395),
);
?>
In this example, if you were to use:
<?php
echo "$cars['car1']['make']<br>";
echo $cars['car3']['engine_cc']";
?>
The output would be:
Toyota
1935
Array Functions
Array functions (PHP manual entry) Some functions, such as sorting an array, are intended to make control arrays easier to use.
Array traversal
In various circumstances, you will need to visit every array element and perform a task upon it.
The simplest and the most used method for this is foreach operator which loops through the whole array and works individually with each key. If a more complex way of traversing the array is needed (for example, performing different actions on pair and impair array elements), following functions which operate on the internal array pointer can be used:
- reset - sets the internal pointer to the first element and returns the first element
- prev - sets the internal pointer to the previous element and returns it
- current - returns the current element; does not change the internal pointer
- next - sets the internal pointer to the next element and returns it
- each - returns the current element; then sets the internal pointer to the next element
- end - sets the internal pointer to the last element and returns the last element
Another possibility is defining a function and applying it to each array element via one of the following functions:
- array_walk - applies a function to each array element
- array_walk_recursive - same, but if the element is itself an array, it will traverse that array too
All text is available under the terms of the GNU Free Documentation License
Source : Wikibooks
|