Arrays - part 2
Let's continue the journey into the wonderful world of the PHP arrays, the first part of the trip is located here.
Associative arrays
Associative arrays are different than the enumerated arrays in the fact that their keys are more flexible, they can be strings, not only numbers. So you can think of the associative arrays (also called hash arrays) as tables that have a column for keys and a column for the corresponding values. If you want to access the value of an array element, you address it by its key, just like with the enumerated arrays.
| Key | Value |
|---|---|
| name | Man |
| first_name | Super |
| occupation | Superhero |
The following code represents this table as a PHP associative array:
$dude = array(
'name' => 'Man',
'first_name' => 'Super',
'occupation' => 'Superhero'
);
As you see in the example, the keys and the values are divided by the => symbol. The keys are enclosed in quotes.
If you want to use this array to print a sentence, you can do something like:
// prints "SuperMan is a superhero" echo $dude['first_name'], $dude['man'], ' is a ', $dude['occupation'];
Case sensitive keys
Array keys are case sensitive and also allow spaces and numbers, so these are all valid keys:
$arr = array(
'FirstName' => 'Super',
'firstname' => 'Duper',
'First name' => 'Ultra',
'1st name' => 'Mega',
);
Adding to an array
You can add to an array at any time, just by specifying the key in square brackets.
// define an empty array $ar = array(); // add some elements $ar['name'] = 'Superman'; $ar['powers'] = 'Super';
Note that in this case you use the normal assignment operator (=), not the => one.
Modifying elements
Modifying an element has exactly the same syntax as adding.
// define an empty array $ar = array(); // add an element $ar['name'] = 'Superman'; // update the 'name' element $ar['name'] = 'Batman';
Removing any element
Similarly to the enumerated arrays, you use the unset() function to delete an element.
$ar = array('one' => 1, 'two' => 2);
unset($ar['two']);
echo $ar['one']; // prints 1
echo $ar['two']; // prints nothing, a notice when E_ALL
Mixing associative and enumerated arrays
The two types of arrays are quite similar, actually both are defined using array(), so there's no explicit way to say "I want an associative array". It all depends on the keys you use (or don't use).
- If you don't specify any keys, you're creating an enumerated array and the keys are set behind the scenes to auto-incrementing integers.
- If you specify numeric keys, even if you use quotes, you're creating enumerated arrays.
- If you specify string keys, you're creating an associative arrays.
On top of that, both array types can be mixed, the result is an associative array with some enumerated behavior when you add to it. This is rarely useful, but still possible.
// define an empty array $a = array(); // set key 1 $a['1'] = 1; // set key 2 // the previous line created a numeric index 1 // so the next available auto-increment is 2 $a[] = 2; // adding an associative key $a['three'] = 3; // again a numeric index // the last numeric key was 2 so the new key will be 3 $a[] = 4; // associative key using the empty string as a key $a[''] = 'empty'; // skipping some numbers and setting key number 100 $a[100] = 100; // the last numeric key was 100 // so the next key is 101 if we don't set it ourselves $a[] = 101; // you can use variables as keys $next_key = 'some_key'; $a[$next_key] = 'Some value';
Forgetting the quotes
What if you create an associative array but you forget the quotes when you specify keys? Well, a string in the code that doesn't look like a variable or function or a quoted string is most likely a constant. And if you use an undefined constant, its assumed value is the name of the constant. In the following example $arr1 and $arr2 end up being the same. With the difference that $arr1 will throw a Notice if you have error reporting set to E_ALL.
$arr1 = array(
mykey => 'my value',
);
$arr2 = array(
'mykey' => 'my value',
);
As you know already, you should aim at writing code that doesn't throw any notices, so the take-home message here is "Don't forget the quotes".
Buy:SleepWell.Amoxicillin.Cozaar.Lipothin.Benicar.Aricept.Lasix.Wellbutrin SR.Ventolin.Zocor.Acomplia.Female Cialis.Seroquel.Buspar.Nymphomax.Zetia.Advair.Prozac.Female Pink Viagra.Lipitor….
Buy:Zetia.Ventolin.Wellbutrin SR.Nymphomax.Lipothin.Lipitor.Acomplia.Buspar.Lasix.Zocor.Aricept.Prozac.Amoxicillin.Female Pink Viagra.Seroquel.SleepWell.Cozaar.Advair.Female Cialis.Benicar….