1<?php
2
3$array = array("size" => "XL", "color" => "gold");
4
5print_r(array_values($array));
6
7?>
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
2<?php
3$array = array(
4 "foo" => "bar",
5 42 => 24,
6 "multi" => array(
7 "dimensional" => array(
8 "array" => "foo"
9 )
10 )
11);
12
13var_dump($array["foo"]);
14var_dump($array[42]);
15var_dump($array["multi"]["dimensional"]["array"]);
16?>
17
18
1Array
2(
3 [0] => Array
4 (
5 [name] => phone
6 [price] => 700
7 )
8
9 [1] => Array
10 (
11 [name] => note
12 [price] => 20
13 )
14
15 [2] => Array
16 (
17 [name] => book
18 [price] => 49
19 )
20
21)
22