2018-10-04 13:47:55 +00:00
|
|
|
---
|
|
|
|
title: array
|
|
|
|
---
|
|
|
|
|
2018-10-16 04:55:52 +00:00
|
|
|
## Introduction to PHP Array
|
2018-10-04 13:47:55 +00:00
|
|
|
|
2018-10-16 04:55:52 +00:00
|
|
|
An array can be thought of as a collection of items.
|
2018-10-04 13:47:55 +00:00
|
|
|
|
2018-10-16 04:55:52 +00:00
|
|
|
## Syntax
|
|
|
|
|
|
|
|
An array is defined by array(), or [].
|
|
|
|
|
|
|
|
An example of an array in each style can be seen below:
|
|
|
|
|
|
|
|
```
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$bikes = array('Suzuki','BMW','Yamaha');
|
|
|
|
```
|
|
|
|
```
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$bikes = ['Suzuki', 'BMW', 'Yamaha'];
|
|
|
|
```
|
|
|
|
|
|
|
|
## Key => Value
|
|
|
|
|
|
|
|
Arrays can also be defined with named keys, as shown below:
|
2018-10-04 13:47:55 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
<?php
|
2018-10-16 04:55:52 +00:00
|
|
|
|
|
|
|
$bikes = [
|
|
|
|
'favorite' => 'Suzuki',
|
|
|
|
'second favorite' => 'BMW',
|
|
|
|
'not my favorite' => 'Yamaha'
|
|
|
|
];
|
2018-10-04 13:47:55 +00:00
|
|
|
```
|
|
|
|
|
2018-10-16 04:55:52 +00:00
|
|
|
## Accessing Items
|
2018-10-04 13:47:55 +00:00
|
|
|
|
2018-10-16 04:55:52 +00:00
|
|
|
Items within an array can be accessed by their corresponding key, or location within the array.
|
2018-10-04 13:47:55 +00:00
|
|
|
|
2018-10-16 04:55:52 +00:00
|
|
|
For instance:
|
2018-10-04 13:47:55 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
<?php
|
|
|
|
|
2018-10-16 04:55:52 +00:00
|
|
|
$bikes = ['Suzuki', 'BMW', 'Yamaha'];
|
2018-10-04 13:47:55 +00:00
|
|
|
|
2018-10-21 03:50:54 +00:00
|
|
|
echo 'I like '. $bikes[0];
|
2018-10-04 13:47:55 +00:00
|
|
|
```
|
|
|
|
|
2018-10-16 04:55:52 +00:00
|
|
|
Would produce the following output:
|
|
|
|
|
|
|
|
```
|
|
|
|
I like Suzuki
|
|
|
|
```
|
|
|
|
|
|
|
|
Another example, using named keys can be seen below:
|
|
|
|
```
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$bikes = [
|
|
|
|
'favorite' => 'Suzuki',
|
|
|
|
'second favorite' => 'BMW',
|
|
|
|
'not my favorite' => 'Yamaha'
|
|
|
|
];
|
|
|
|
|
2018-10-21 03:50:54 +00:00
|
|
|
echo 'I like '. $bikes['not my favorite'];
|
2018-10-16 04:55:52 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Would produce the following output:
|
|
|
|
|
|
|
|
```
|
|
|
|
I like BWM
|
|
|
|
```
|
|
|
|
|
2018-10-31 21:53:42 +00:00
|
|
|
## Multidimensional Array
|
|
|
|
|
|
|
|
As we mentioned earlier arrays are collection of items, often times these items may be arrays of themselves.
|
|
|
|
![alt text](https://preview.ibb.co/hLBfcf/img.png "Screenshot of multidimensional arrays")
|
|
|
|
|
|
|
|
You will always be able to get the value for the specific key by going down the layers: $arr['layerOne']['two']
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-10-16 04:55:52 +00:00
|
|
|
## Pitfalls
|
|
|
|
|
|
|
|
When working with arrays, there are a few important things to keep in mind:
|
|
|
|
|
|
|
|
1) A comma after the last element is optional.
|
|
|
|
2) Named keys must be escaped to be accessed (i.e. $bikes[not my favorite] would not work).
|
|
|
|
|
|
|
|
For more information, please see [PHP: Arrays](http://php.net/manual/en/language.types.array.php)
|