PHP arrays
Arrays are a very important concept in PHP as well as many other programming languages. An array is a special type of variable which can hold a list of information.
This tutorial focuses on:
- The necessity of arrays
- Numeric arrays
- Associative arrays
The necessity of arrays
Imagine you are writing a program that has three variables which contain the names of cars, your code might look like this:
With Arrays, it is much simpler. An array gives you the ability to group together related variables into one set. Arrays are special variables which hold lists of information. Therefore, instead of the code above you can declare a 'cars' array:
Numeric arrays
The numeric array is an array which associates an index number to a certain value.
Creating a numeric array
A numeric array is created by initializing a variable which will be the name of the array together with the array function
Creating an array this way, you can add as many values as you want into it. Alternatively, you can specify an array to be of a particular size.
In the above example, an array named "animals" that can store 4 elements is created.
Adding values to a numeric array
Once you create a numeric array, you can add values to it by specifying the array name, an index in the array and a value to be placed in that index.
In the above example, an array named "animals" is declared. Four values are added to it.
NOTE: Array indexes start at 0. So the 1st element of an array would be at index 0, the 5th element of an array would be at index at 4, and so on.
Another way to assign values to an array involves doing so during the array's initialization.
Accessing a numeric arrays elements
To access a numeric arrays elements, refer to the array with the appropriate index number in brackets.
Modifying a numeric arrays elements
To modify a numeric arrays elements, refer to the array with the appropriate index number in brackets of the value you want to change and set it to the new value.
Associative arrays
The associative array is an array which associates an index value (instead of a number) to a certain value.
Creating an associative array
An associative array is created by initializing a variable which will be the name of the array together with the array function.
Creating an array this way, you can add as many values as you want into it. Alternatively, you can specify an array to be of a particular size.
In the above example, an array named "cars" that can store 4 elements is created.
Adding values to an associative array
Once you create an associative array, you can add values to it by specifying the array name, an index value in the array and a value to be placed in that index.
Another way to assign values to an associative array involves doing so during the array's initialization.
Accessing an associative arrays elements
To access an associative arrays elements, refer to the array with the appropriate index value in brackets.
Modifying an associative arrays elements
To modify an associative arrays elements, refer to the array with the appropriate index value in brackets of the value you want to change and set it to the new value.
