Arrays In PHP : An Introduction
Here I wanted to explain what arrays are and how they work in PHP.
Arrays are basically like a “list” of related values, stored in a single variable. There are two types of arrays in PHP: INDEXED and ASSOCIATIVE.
INDEXED ARRAY
An INDEXED array is important when you want to store values in an array and the order is important to you. Once stored, you can call these values by the index number of where it is in the order of the values currently stored. I will create an array of the Teenage Mutant Ninja Turtles using two methods. These methods produce the same result, choose whichever you prefer to use:
$turtles1 = array (‘Leonardo’, ‘Donatello’, ‘Raphael’, ‘Michaelangelo’);
$turtles2 = [‘Leonardo’, ‘Donatello’, ‘Raphael’, ‘Michaelangelo’];
Note that you can’t use “echo” to print an array to the screen because PHP doesn’t automatically convert arrays to strings. To print an array to the screen use the inbuildt function: print_r( ); like so:
print_r($turtles1);
which will return:
Array ( [0] => Leonardo [1] => Donatello [2] => Raphael [3] => Michaelangelo )
This prints to the screen the contents of the array and a number for each individual array element, starting at 0. This number is the INDEX of the array. This INDEX number can be used to identify and access each of these individual array elements.
To add elements to the end of an array do:
$turtles2[ ] = ‘Splinter’;
To specify the position of the item you’re adding to the array, put the index number you want it to be into the square brackets:
$turtles2[3] = ‘Shredder’;
To select an item in the array based on its INDEX value:
$turtles2[3];
this will return:
Michaelangelo
You can have multiple datatypes in an array.. even another array!
$mixedArray = array(1, “fox”, array(“x”, 2, 3, 4, 5));
to get the value from the second array
$parentArrayName[indexOfChildArray][indexOfValueInChildArray]:
$mixedArray[2][0];
this will give you “x”
ASSOCIATIVE ARRAY
This is good when you don’t care about the order of the values inside an array. In an associative array, and item is identified by a STRING instead of by its INDEX (as with an INDEXED array). This STRING takes the form of a KEY VALUE PAIR. Each associative array has a “key”, and each of these keys has a “value”. It is written like this:
$associative_array = [
‘key1’ => ‘value1’
‘key2’ => ‘value2’
];
or
$associative_array = array (
‘key’ => ‘value’
‘key2’ => ‘value2’
);
so, therefore, if we applied this to the Teenage Mutant Ninja Turtles:
$turtles = [
‘Leonardo’ => ‘lead’,
‘Donatello’ => ‘does machines’,
‘Raphael’ => ‘cool but rude’,
];
to print a value of a key, put the key into square brackets after the name of the array:
echo $turtles[‘Raphael’];
to print entire array, use print_r instead of echo to print to screen
print_r($turtles);
this returns the following:
Array ( [Leonardo] => lead [Donatello] => does machines [Raphael] => cool but rude )
to add a key value pair to an associative array:
$turtles [‘Michaelangelo’] = ‘party dude’;
to echo the contents of an associative array into an echoed string, you need to put curly braces around the array.
echo “Michaelangelo is a {$turtles[‘Michaelangelo’]}”;
CONCLUSION
This should be everything you need to know about arrays in PHP to get you started. Have fun!