|
| WHAT ARE ARRAYS?
Arrays are super complex variables that can store a great amount of values which are indexed and easily referenced.
Arrays make it easier to pass multiple values between pages, code and functions.
PHP arrays are associative - this means that the element values are stored in the array in association with their key values and not in a linear vector format.
Why use arrays?
Arrays make it easier for the PHP programmer to handle large amounts of values. Consider the following:
Assume you want to store the personal details of a person:
Just using variables:
$person_name="John";
$person_email="john@somesite.com";
$person_country="India";
You notice that for each characteristic, we have separate values and separate variables.
If you are using these variables further in your code, passing to another page or passing to functions, you will still have to reference each variable individually which can become messy and confusing.
Now, let's see how easy and simpler it gets with arrays. We can store all of the above three values and variables in one array variable as follows:
$person=array('name' => 'John', 'email' => 'john@somesite.com', 'country' => 'India');
When $person is now used elsewhere in your code, function or passed to a different page, the values can be retrieved as follows:
$person['name'] will be equal to: John
$person['email'] will be equal to: john@somesite.com
$person['country'] will be equal to: India
See also:
|
|