Working with Arrays in PHP. Free online PHP and MySQL Web Database Programming Tutorial...
|Lessons||Working with Arrays in PHP|
What is the solution?
We can use arrays to store multiple values in a single variable. Instead of having many similar variables, we can store the data as elements in an array. Each element in the array has its own ID so that it can be easily accessed.
An array in PHP is an ordered map. An array can be created by the array() construct. It takes a certain number of comma-separated values. An example is as under
$days = array("Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday");
echo $days; // outputs Friday
The above code declares an array named $days, with seven elements. We can access the array elements using the [index] notation. Note that PHP arrays have 0 based indices i.e. the first element of an array is at index 0, second at 1 and son on.
We can also define the above array as under:
$days = "Friday";
$days = "Saturday";
$days = "Sunday";
$days = "Monday";
$days = "Tuesday";
$days = "Wednesday";
$days = "Thursday";
echo $days; // outputs Thursday
Next: PHP Array Types