Today I learned about arrays, which is one of the more important ways to use PHP data. PHP's Array function is numerous, below is my study summary, takes this to remember, facilitates later ...
one, array definition:
The definition of an array is defined using the array () method, and an empty array can be defined:
Copy Code code as follows:
<?php
$number = Array (1,3,5,7,9);
Define an empty array
$result = Array ();
$color =array ("Red", "Blue", "green");
Custom Key values
$language = (1=> "中文版",3=> "Chinese",5=> "Franch");
Defining two-dimensional arrays
$two = Array (
"Color" =>array ("Red", "blue"),//ending with a comma
"Week" =>array ("Monday", "Friday")//last sentence without punctuation
);
?>
second, create an array:
The created array contains functions that have a compact (),
1.compact () function-converts one or more variables (including arrays) to an array:
Array compact (mixed $varname [, mixed $ ...])
Copy Code code as follows:
? Php
$number = "1,3,5,7,9";
$string = "I ' m phper";
$array = Array ("and", "you?");
$newArray = Compact ("Number", "string", "array");
Print_r ($newArray);
?>
The compact () function is used to convert two or more variables to an array, and, of course, to include arrays of variables. The argument is the name of the variable, not the full name.
The opposite function is the extract () function that converts an array to a single string, a key value as its string name, and an array value as the value of the string.
Run Result:
Copy Code code as follows:
Array ([number] => 1,3,5,7,9 [string] => I ' m phper [array] => array ([0] => and [1] => you?)
2.array_combine ()--resets two arrays into an array, one for key values:
Array Array_combine (array $keys, array $values)
Copy Code code as follows:
? Php
$number = Array ("1", "3", "5", "7", "9");
$array = Array ("I", "Am", "A", "PHP", "er");
$newArray = Array_combine ($number, $array);
Print_r ($newArray);
?>
Array_combine function Not much said, who read all understand
Run Result:
Array ([1] => I [3] => Am [5] => A [7] => PHP [9] => er)
3.range () function--Creates an array of the specified range:
Not much to say, directly on the example--
Copy Code code as follows:
? Php
$array 1 = range (0,100,10);//0 is the starting value, 100 is the ending value, 10 is the step value (the default step value is 1).
Print_r ($array 1);
echo "<br/>";
$array 2 = Range ("A", "Z");
Print_r ($array 2);
echo "<br/>";
$array 3 = range ("Z", "a");
Print_r ($array 3);
?>
The default step value for the range () function is 1!
Run Result:
Copy Code code as follows:
Array ([0] => 0 [1] => [2] => [3] => [4] => [5] => [6] => [7] => 8 0 [9] => [a] => 100)
Array ([0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6] => G [7] => H [8] => I [9] =& Gt J [A] => K [one] => L [[P] => M [[] => N [P] => O [] => [[] => Q [] => R [S] => [19 ] => T [m] => U [[] => V [a] => W [a] => X [[] => Y [] => Z)
Array ([0] => Z [1] => y [2] => x [3] => W [4] => v [5] => u [6] => T [7] => s [8] => R [9] =& Gt Q [A] => p [one] => o [[a] => n [[] => m [p] => l [[] => k [[] => J [] => I [[] => H [19 ] => G [m] => F [a] => e [a] => d [a] => C [a] => b [d] => a)
4.array_fill () function--Populate array functions:
Copy Code code as follows:
? Php
$array = range (1,10);
$fillarray = Range ("A", "D");
$arrayFilled = Array_fill (0,5, $fillarray);//The $fillarray here can be strings, such as "test".
echo "<pre>";
Print_r ($arrayFilled);
echo "</pre>";
$keys = Array ("string", "2", 9, "SDK", "PK");
$array 2 = Array_fill_keys ($keys, "testing");
echo "<pre>";
Print_r ($array 2);
echo "</pre>";
?>
Run Result:
Copy Code code as follows:
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => C
[3] => D
)
[1] => Array
(
[0] => a
[1] => b
[2] => C
[3] => D
)
[2] => Array
(
[0] => a
[1] => b
[2] => C
[3] => D
)
[3] => Array
(
[0] => a
[1] => b
[2] => C
[3] => D
)
[4] => Array
(
[0] => a
[1] => b
[2] => C
[3] => D
)
)
Array
(
[String] => testing
[2] => testing
[9] => Testing
[SDK] => testing
[PK] => testing
)
Second, the array of traversal:
1.foreach Traversal:
foreach (array_expression as $value) {}
foreach (array_expression as $key => $value) {}
Less gossip, above instance:
Copy Code code as follows:
? Php
$speed = Array (50,120,180,240,380);
foreach ($speed as $keys => $values) {
echo $keys. " => ". $values." <br/> ";
}
?>
Run Result:
Copy Code code as follows:
0=>50
1=>120
2=>180
3=>240
4=>380
2.while Loop Traversal:
While loop traversal is generally combined with the list function, the following is an instance
Copy Code code as follows:
? Php
$staff = Array (
Array ("Name", "Gender", "age"),
Array ("Xiao Zhang", "male", 24),
Array ("Xiao Wang", "female", 25),
Array ("Xiao Li", "male", 23)
);
echo "<table border=2>";
while (the list ($keys, $value) = each ($staff)) {
List ($name, $sex, $age) = $value;
echo "<tr><td> $name </td><td> $sex </td><td> $age </td></tr>";
}
echo "</table>";
?>
Run Result:
| Name |
Gender |
Age |
| Xiao Zhang |
Man |
24 |
| Xiao Wang |
Woman |
25 |
| Xiao Li |
Man |
23 |
3.for Loop Traversal:
Copy Code code as follows:
? Php
$speed = range (0,220,20);
for ($i =0; $i <count ($speed); $i + +) {
echo $speed [$i]. " ";
}
?>
Run Result:
Copy Code code as follows:
0 20 40 60 80 100 120 140 160 180 200-220