1 // Echo's notation 2 Echo "5 + ten =" . " <br> "; 3 Echo "5+10 <br>"; 4 Echo "5+10 = {$a} <br>";
In PHP, there are three types of arrays:
- Indexed array-an array with a numeric index
- Associative array-An array with the specified key
- Multidimensional arrays-An array that contains one or more arrays
PHP indexed Array
There are two methods of creating an indexed array:
Indexes are automatically assigned (index starting from 0):
$cars=array("Volvo", "BMW", "SAAB");
Or you can manually assign an index:
1 $cars [0]= "Volvo"; 2 $cars [1]= "BMW"; 3 $cars [2]= "SAAB";
PHP Associative array
An associative array is an array that uses the specified key that you assign to the array.
There are two ways to create an associative array:
$age=array("Peter" and "+", "Ben" = "PNs", "Joe" = "43");
Or:
$age [' Peter ']= '; $age [' Ben ']= "Notoginseng"; $age [' Joe ']= ' 43 ';
Iterating through an indexed array
To iterate through and output all the values of an indexed array, you can use a for loop, like this:
Instance
<? PHP $cars=array("Volvo", "BMW", "SAAB"); $arrlength=count($cars); for ($x= 0; $x<$arrlength; $x+ +) {echo$cars[$x] ; Echo "<br>";}? >
Traversing associative arrays
To traverse and output all the values of an associative array, you can use a foreach loop like this:
Instance
<? PHP $age=array("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "+"); foreach ($ageas$x=$x _value) { echo$x$x _value; Echo "<br>";}? >
php--02