Learning an array today is one of the more important ways to use PHP data. PHP array functions are numerous, the following is my study of the summary, take this to remember, easy to learn later ...
One, the array definition:
The definition of an array is defined using the array () method, and an empty array can be defined:
Copy CodeThe code is as follows:
$number = Array (1,3,5,7,9);
Defining an empty array
$result = Array ();
$color =array ("Red", "Blue", "green");
Custom Key values
$language = (1=> "中文版",3=> "Chinese",5=> "Franch");
Defining a two-dimensional array
$two = Array (
"Color" =>array ("Red", "blue"),//end with a comma
"Week" =>array ("Monday", "Friday")//last sentence without punctuation
);
?>
second, create an array:
The created array contains functions that have the compact (),
1.compact () function--converts one or more variables (containing arrays) to an array:
Array compact (mixed $varname [, mixed $ ...]
Copy CodeThe code is as follows:
$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 arrays and, of course, to array variables. Its argument is the name of the variable, not the full name.
The opposite function is that extract () functions as the name implies to convert an array to a single string, a key value as its string names, and an array value as the value of a string.
Operation Result:
Copy CodeThe code is as follows:
Array ([number] = 1,3,5,7,9 [string] + I ' m phper [array] = = Array ([0] = = and [1] = =))
2.array_combine ()--two arrays are re-formed into an array, one for the key value:
Array Array_combine (array $keys, array $values)
Copy CodeThe code is as follows:
$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 to say, who read all understand
Operation 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 CodeThe code is as follows:
$array 1 = range (0,100,10),//0 is the starting value, 100 is the end value, and 10 is a stepping value (the default stepping value is 1).
Print_r ($array 1);
Echo
";
$array 2 = Range ("A", "Z");
Print_r ($array 2);
echo "
";
$array 3 = range ("Z", "a");
Print_r ($array 3);
?>
The default step value for the range () function is 1!
Operation Result:
Copy CodeThe code is as follows:
Array ([0] = 0 [1] = [2] = [3] = [4] [5] [6] = [7] = [8] = 8 0 [9] = [ten] = 100)
Array ([0] = A [1] = B [2] = = C [3] = + D [4] = E [5] + F [6] = G [7] = H [8] = I [9] =& Gt J [Ten] + K [one] = L [N] = M [19] [+] [+] = O [+] = P [+] = Q [+] = R [+] ] [+] = [+] = U [+] = V [X] = W [+] + × [] = Y [+] = Z)
Array ([0] = Z [1] = y [2] + x [3] = w [4] = v [5] = = u [6] = [7] = [8] = R [9] =& Gt Q [Ten] = p [one] = O [[j] = n [19] = m [+] = l [[+] + k [+] [+] [+] [[]] ] = g [+] = f [+] = e [+] + d [+] = c [+] = b [+] = a)
4.array_fill () Function--fills the array function:
Copy CodeThe code is as follows:
$array = range (1,10);
$fillarray = Range ("A", "D");
$arrayFilled = Array_fill (0,5, $fillarray);//The $fillarray here can be a string, such as "test".
echo "
echo "
";
$keys = Array ("string", "2", 9, "SDK", "PK");
$array 2 = Array_fill_keys ($keys, "testing");
echo "
echo "
";
?>
Operation Result:
Copy CodeThe code is 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 traversal:
1.foreach Traversal:
foreach (array_expression as $value) {}
foreach (array_expression as $key = $value) {}
Gossip less, on the example:
Copy CodeThe code is as follows:
$speed = Array (50,120,180,240,380);
foreach ($speed as $keys = = $values) {
echo $keys. " = ". $values."
";
}
?>
Operation Result:
Copy CodeThe code is 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 example
Copy CodeThe code is as follows:
$staff = Array (
Array ("Name", "Gender", "age"),
Array ("Xiao Zhang", "male", 24),
Array ("Xiao Wang", "female", 25),
Array ("Xiao Li", "male", 23)
);
echo "
"; while (list ($keys, $value) = each ($staff)) {list ($name, $sex, $age) = $value; echo "
$name |
$sex |
$age |
"; } echo "
";
?>
Operation Result:
Name |
Gender |
Age |
Xiao Zhang |
Man |
24 |
Xiao Wang |
Woman |
25 |
Xiao Li |
Man |
23 |
3.for Loop Traversal:
Copy CodeThe code is as follows:
$speed = range (0,220,20);
for ($i =0; $i echo $speed [$i]. " ";
}
?>
Run Result:
copy code 0 + + + + + + +
htt p://www.bkjia.com/phpjc/323455.html www.bkjia.com true http:// www.bkjia.com/PHPjc/323455.html techarticle Learning an array today is one of the more important ways to use PHP data. PHP array functions are numerous, the following is my study of the summary, take this to remember, easy to learn later ... A ...