Array in PHP
Array Introduction
An array in PHP is same as JavaScript as it can hold more than one value at a time.
In PHP, the array() function is used to create an array:
array();
Types Of Arrays
In PHP, there are three types of arrays:
-
Indexed arrays - Arrays with numeric index
-
Associative arrays - Arrays with named keys
-
Multidimensional arrays - Arrays containing one or more arrays
Indexed Arrays
- Let PHP assign the index automatically (index always starts at 0)
$seasons=array(“Summer”,”Winter”,”Rainy”);
-
Assign the index manually.
$seasons[0]=“Summer”;
$seasons[1]=“Winter";
$seasons[2]=“Rainy";
Accessing Indexed Arrays
To access array elements we can use their index
$seasons=array(“Summer”,”Winter”,”Rainy”);
echo $seasons[0] . “<br>”;
echo $seasons[1] . “<br>”;
echo $seasons[2] . “<br>”;
Using The count( ) Function
<?php
$seasons=array(“summer“,”winter”,”rainy”);
echo count($seasons);
?>