Вы находитесь на странице: 1из 2

1 PHP 5 Arrays

2
3 An array stores multiple values in one single variable:
4 Example
5 <?php
6 $cars = array("Volvo", "BMW", "Toyota");
7 echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
8 ?>
9 What is an Array?
10
11 An array is a special variable, which can hold more than one value at a time.
12
13 If you have a list of items (a list of car names, for example), storing the cars in
single variables could look like this:
14 $cars1 = "Volvo";
15 $cars2 = "BMW";
16 $cars3 = "Toyota";
17
18 However, what if you want to loop through the cars and find a specific one? And what
if you had not 3 cars, but 300?
19
20 The solution is to create an array!
21
22 An array can hold many values under a single name, and you can access the values by
referring to an index number.
23 Create an Array in PHP
24
25 In PHP, the array() function is used to create an array:
26 array();
27
28 In PHP, there are three types of arrays:
29
30 Indexed arrays - Arrays with a numeric index
31 Associative arrays - Arrays with named keys
32 Multidimensional arrays - Arrays containing one or more arrays
33
34 PHP Indexed Arrays
35
36 There are two ways to create indexed arrays:
37
38 The index can be assigned automatically (index always starts at 0), like this:
39 $cars = array("Volvo", "BMW", "Toyota");
40
41 or the index can be assigned manually:
42 $cars[0] = "Volvo";
43 $cars[1] = "BMW";
44 $cars[2] = "Toyota";
45
46 The following example creates an indexed array named $cars, assigns three elements
to it, and then prints a text containing the array values:
47 Example
48 <?php
49 $cars = array("Volvo", "BMW", "Toyota");
50 echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
51 ?>
52 Get The Length of an Array - The count() Function
53
54 The count() function is used to return the length (the number of elements) of an
array:
55 Example
56 <?php
57 $cars = array("Volvo", "BMW", "Toyota");
58 echo count($cars);
59 ?>
60 Loop Through an Indexed Array
61
62 To loop through and print all the values of an indexed array, you could use a for
loop, like this:
63 Example
64 <?php
65 $cars = array("Volvo", "BMW", "Toyota");
66 $arrlength = count($cars);
67
68 for($x = 0; $x < $arrlength; $x++) {
69 echo $cars[$x];
70 echo "<br>";
71 }
72 ?>
73 PHP Associative Arrays
74
75 Associative arrays are arrays that use named keys that you assign to them.
76
77 There are two ways to create an associative array:
78 $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
79
80 or:
81 $age['Peter'] = "35";
82 $age['Ben'] = "37";
83 $age['Joe'] = "43";
84
85 The named keys can then be used in a script:
86 Example
87 <?php
88 $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
89 echo "Peter is " . $age['Peter'] . " years old.";
90 ?>
91 Loop Through an Associative Array
92
93 To loop through and print all the values of an associative array, you could use a
foreach loop, like this:
94 Example
95 <?php
96 $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
97
98 foreach($age as $x => $x_value) {
99 echo "Key=" . $x . ", Value=" . $x_value;
100 echo "<br>";
101 }
102 ?>
103 Multidimensional Arrays
104
105 Multidimensional arrays will be explained in the PHP advanced section.
106 Complete PHP Array Reference
107
108 For a complete reference of all array functions, go to our complete PHP Array
Reference.
109
110 The reference contains a brief description, and examples of use, for each function!

Вам также может понравиться