1
2<?php
3$array = array(
4 "foo" => "bar",
5 "bar" => "foo",
6);
7
8// as of PHP 5.4
9$array = [
10 "foo" => "bar",
11 "bar" => "foo",
12];
13?>
14
15
1#Arrays
2
3<?php
4 #array is a variable that holds multiple values
5 /*
6 Three types of arrays
7 - Indexed
8 - associative
9 - Multi-dimensional
10 */
11 // Indexed array is the most common and easiest
12 $people = array('Kevin', 'Jeremy ', 'Sara');
13 $ids = array(23, 55, 12);
14 $cars = ['Honda', ' Toyota', 'Ford']; // also an array
15 //add to an array
16 $cars[3] = ' Audi';
17 // you can use empty brackets and it will be added to the last one
18 $cars[] = ' Chevy';
19
20 echo $people[1];
21 echo $ids[1];
22 echo $cars[1];
23 echo $cars[3];
24 echo $cars[4];
25 echo count($cars);
26 //you can also print the entire array
27 print_r($cars);
28 //to look at data type
29 echo var_dump($cars);
30
31 //Associative Arrays key pairs
32 $people1 = array('Kevin' => 35, 'Jeremy' => 23, 'Sara' => 19);
33 $ids1 = array(23 => 'Kevin', 55 => 'Jeremy', 12 => 'Sara');
34 echo $people1['Kevin'];
35 echo $ids1[23];
36 //add to these types of arrays
37 $people1['Jill'] = 44;
38 echo $people1['Jill'];
39 print_r($people1);
40 var_dump($people1);
41
42 //Multi-Dimensional Arrays aka an array within an array
43
44 $cars2 = array(
45 array('Honda',20,10),
46 array('Toyota',30,20),
47 array('Ford',23,12)
48 );
49
50 echo $cars2[1][0];
51
52
53?>
1<?php
2$array = array(
3 "foo" => "bar",
4 "bar" => "foo",
5);
6
7// as of PHP 5.4
8$array = [
9 "foo" => "bar",
10 "bar" => "foo",
11];
12?>
13