Arrays
An array is a fixed list of values which can be accessed with their corresponding key. By default the keys are assigned an integer, starting from 0 and going up in 1's, but, you can give them their own unique name which we will get into later. Each value in the list is seperated by a comma, the values can be any kind of data - strings, integers and so on. You can even have arrays inside of arrays which, again, we will get into later.
Below we are simply creating an array and naming it $myArray. This array will have 11 values assigned in it. In each we will have an integer or a string.
PHP Code:
$myArray = array(4, 14, 56, 35, "dean", 19, 17, "dog", 2, 135, 13);
Now, to retrieve a value we specify the name we gave to the array and the key assigned to the value:
PHP Code:
echo $myArray[2];
We named our array $myArray, so, to retrieve data from it we give the name we gave to the array and the specific key in which has been assigned to it.
The above example will echo out the data in key name 2 which is 56.
Why 56? Array keys start from 0 not from 1 so if we wanted to get the number 4, which has the key 0 since it is the first value we identified in the array we would do the following:
PHP Code:
echo $myArray[0];
Arrays inside of Arrays
Again let's firstly create our array except this time we're going to have an array inside of it.
PHP Code:
$myOtherArray = array(1, 2, 3, 4, array("a", "b", "c"));
Retrieving values from an array inside of an array is just as simple as well. We named this array $myOtherArray, so, if we want to get values from the array inside of it we do this:
PHP Code:
echo $myOtherArray[4][1];
The key named 4 contains our array so we specify that first then we specify the key in which the value is we want to obtain, which is 1. This will echo 'b' back to us.
Giving Keys Names
Giving keys their own unique names.
In this case, 'my_name' will contain the string "Dean" and 'number' will contain the integer 1.
PHP Code:
$namedArray = array("my_name" => "Dean", "number" => 1);
To retrieve the data from a named array we do the following:
PHP Code:
echo $namedArray["my_name"];
This will echo back to us what's in key named 'my_name', in this case, Dean. We can do the exact same for retrieving the data in any other named key:
PHP Code:
echo $namedArray["number"];
Adding to arrays
Like everything before, this is just as simple. Here's how it's done...
Let's define our array first.
PHP Code:
$addToArray = (1, "dog", 3, 4, "dean");
Now to add data to an array, we do this.
PHP Code:
//$addToArray will be the array name
$addToArray[] = 11;
$addToArray[] = "hey";
Above, we added 2 pieces of data to the array 11 and hey.
The array will now look like this...
PHP Code:
$addToArray[0]; //Outputs 1
$addToArray[1]; //Outputs dog
$addToArray[2]; //Outputs 3
$addToArray[3]; //Outputs 4
$addToArray[4]; //Outputs dean
//Now our new pieces of data, it incremements by 1 each time, so, 5 will output 11 and 6 will output hey
$addToArray[5]; //Outputs 11
$addToArray[6]; //Outputs hey
PHP 5.4 Arrays
As of PHP 5.4, you can define arrays using a shorter syntax.
PHP Code:
$xboxMb = ['XboxMB', 'is', 'epic'];
$userGroups = [
0 => 'Guest',
6 => 'Diamond',
3 => 'Developer'
];
$students = [
648 => [
'name' => 'John Smith',
'average' => 87.5
],
998 => [
'name' => 'Derp Derp',
'average' => 31.0
]
];