1$array = array(
2 "foo" => "bar",
3 "bar" => "foo",
4);
1$array = array(
2 "foo" => "bar",
3 "bar" => "foo",
4);
5
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$my_array = array();
1// sequential array
2$arr = array(
3 'v1', // $arr[0] == 'v1'
4 'v2', // $arr[1] == 'v2'
5 'v3' // $arr[2] == 'v3'
6);
7// or (since PHP 5.4)
8$arr = [
9 'v1', // $arr[0] == 'v1'
10 'v2', // $arr[1] == 'v2'
11 'v3' // $arr[2] == 'v3'
12];
13
14// key accessed array (map like)
15$arr = array(
16 'k1' => 'v1', // $arr['k1'] == 'v1'
17 'k2' => 'v2', // $arr['k2'] == 'v2'
18 'k3' => 'v3' // $arr['k3'] == 'v3'
19);
20// or (since PHP 5.4)
21$arr = [
22 'k1' => 'v1', // $arr['k1'] == 'v1'
23 'k2' => 'v2', // $arr['k2'] == 'v2'
24 'k3' => 'v3' // $arr['k3'] == 'v3'
25];
1
2<?php
3$array = array("foo", "bar", "hello", "world");
4var_dump($array);
5?>
6
7