![]() |
|
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[0]; // 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[0] = "Friday"; $days[1] = "Saturday"; $days[2] = "Sunday"; $days[3] = "Monday"; $days[4] = "Tuesday"; $days[5] = "Wednesday"; $days[6] = "Thursday"; echo $days[6]; // outputs Thursday
Next: PHP Array Types
|