Simply an Array is a group of the data items or we can say An Array is a set of variables that all have same name. for example we can define an array Capitals to represent a set of the capitals name of the some countries. See the following example:
- $Capitals[1] = "New Delhi";
- $Capitals[2] = "Washington";
Assign values to an array in any order
We can assign values to array in any order, see the following lines of code:
- $Capitals[5] = "Tokyo";
- $Capitals[3] = "Sydney";
Use Characters as Index to assign values
From the above we can see that we are using numeric value to assign values to the array Capitals. We can use characters instead. The characters are inside quotes in the brackets, see the following example:
- $Capitals[“A”] = "Londan";
- $Capitals[“B”] = "Paris";
And we can get these element from the array as :
- Echo $capitals["A"];
An array in PHP hold different data types and variables.
An array in PHP hold different data types and variables. See the following example:
- $Num[1]=5;
- $Num[2]= "One";
- $Num[3]=$var;
Initialization of Arrays
In PHP we can create an array without indexing. see this:
- $Capitals[] = "New Delhi";
- $Capitals[] = "Washington";
PHP do it automatically and another another way to initialize an array is to use explicit index values . see the following example:
- $Capitals[0] = "New Delhi";
- $Capitals[1] = "Washington";
One thing that you can see in the all above code, we can create an array without specify its size.
And Now another way to initialize an array: we can initialize an array by array construct, see the following code:
$Capitals= array( “New Delhi”,”Washington”);