| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | --- | 
					
						
							|  |  |  | title: array | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | ## Introduction to PHP Array
 | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | An array can be thought of as a collection of items. | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | ## Syntax
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 05:50:57 +01:00
										 |  |  | An array is defined by `array()`, or `[]`. | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | An example of an array in each style can be seen below: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 04:58:56 +01:00
										 |  |  | ```php | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $bikes = array('Suzuki','BMW','Yamaha'); | 
					
						
							| 
									
										
										
										
											2018-12-17 05:50:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // OR | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | $bikes = ['Suzuki', 'BMW', 'Yamaha']; | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 05:50:57 +01:00
										 |  |  | ## Associative array (key => value)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 06:14:22 +02:00
										 |  |  | PHP arrays can store more than one type of value at a time: | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $arr = array('Suzuki', 3.1415, false, -273); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | As you can see there is a string, a float number, a boolean valuea and an integer number. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | Arrays can also be defined with named keys, as shown below: | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 04:58:56 +01:00
										 |  |  | ```php | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | $bikes = [ | 
					
						
							|  |  |  |     'favorite'        => 'Suzuki', | 
					
						
							|  |  |  |     'second favorite' => 'BMW', | 
					
						
							|  |  |  |     'not my favorite' => 'Yamaha' | 
					
						
							|  |  |  | ]; | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | ## Accessing Items
 | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | Items within an array can be accessed by their corresponding key, or location within the array. | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | For instance: | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 04:58:56 +01:00
										 |  |  | ```php | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | $bikes = ['Suzuki', 'BMW', 'Yamaha']; | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-21 10:50:54 +07:00
										 |  |  | echo 'I like '. $bikes[0]; | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | Would produce the following output: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | I like Suzuki | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Another example, using named keys can be seen below: | 
					
						
							| 
									
										
										
										
											2018-12-17 04:58:56 +01:00
										 |  |  | ```php | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $bikes = [ | 
					
						
							|  |  |  |     'favorite'        => 'Suzuki', | 
					
						
							|  |  |  |     'second favorite' => 'BMW', | 
					
						
							|  |  |  |     'not my favorite' => 'Yamaha' | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-21 10:50:54 +07:00
										 |  |  | echo 'I like '. $bikes['not my favorite']; | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Would produce the following output: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-12-17 05:50:57 +01:00
										 |  |  | I like Yamaha | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 06:06:35 +01:00
										 |  |  | ## Add Item
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Is possible to add any item to an existing array. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | An example of addition can be seen below: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $bikes = array('Suzuki', 'BMW'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $bikes[] = 'Yamaha'; | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Another example, using named keys can be seen below: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $bikes = [ | 
					
						
							|  |  |  |     'favorite'        => 'Suzuki', | 
					
						
							|  |  |  |     'second favorite' => 'BMW' | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $bikes['not my favorite'] = 'Yamaha'; | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 22:53:42 +01:00
										 |  |  | ## Multidimensional Array
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | As we mentioned earlier arrays are collection of items, often times these items may be arrays of themselves.  | 
					
						
							|  |  |  |  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You will always be able to get the value for the specific key by going down the layers: $arr['layerOne']['two'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04: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. | 
					
						
							| 
									
										
										
										
											2018-12-17 17:14:14 +00:00
										 |  |  | 2) Named keys must use quotes to be accessed (i.e. $bikes[not my favorite] would not work). | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:52 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | For more information, please see [PHP: Arrays](http://php.net/manual/en/language.types.array.php) | 
					
						
							| 
									
										
										
										
											2018-12-17 06:07:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Length of an Array
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The count() function is used to return the length (the number of elements) of an array: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  |     $cars = array("Volvo", "BMW", "Toyota"); | 
					
						
							|  |  |  |     echo count($cars); | 
					
						
							|  |  |  | ?> |