Arrays in PHP
1. Definition of the array:
Explicit mode: $arr = Array (1,2,3,4,5);
$arr 1 = Array (' name ' = ' DQRCSC ', ' age ' = ' 24 ');
Implicit way: $arr []=1; $arr []=2;
Note: Array () is a language structure that is used to literally represent an array, not a regular function.
2. About the index of the PHP array:
The syntax "index = values", separated by commas, defines the index and value. The index can be a string or a number.
If the index is omitted, an integer index starting from 0 is automatically generated. If the index is an integer, the next resulting index will be the current largest integer index + 1.
Note If you define two identical indexes, the next one overwrites the previous one.
In addition, the index of the PHP array has the following translation rules:
In addition, key will have the following casts:
1) A string containing a valid integer value is converted to an integral type. For example, the key name "8" will actually be stored as 8. However, "08" does not cast because it is not a valid decimal value.
2) floating-point numbers are also converted to integers, meaning that their fractional parts are removed. For example, key name 8.7 will actually be stored as 8.
3) The Boolean value is also converted to an integer type. That is, the key name true is actually stored as 1 and the key name false is stored as 0.
4) NULL is converted to an empty string, that is, the key name null is actually stored as "".
5) Arrays and objects cannot be used as key names. Insisting on doing so will result in a warning: illegal offset type.
<?php
$array = Array (
1 = "a",
"1" = "B",//Index converted to Integer 1
1.5 = "C",//index converted to integral type 1
true = "D",//index converted to integral type 1
);
Var_dump ($array);
?>
The above routines will output:
Array (1) {
[1]=>
String (1) "D"
}
3. Traversal of the array:
COUNT (): Returns the number of array elements (length)
1) Use the For loop to iterate through the indexed array:
$len = count ($arr);
for ($i =0; $i < $len; $i + +) {$arr [$i] ...}
2) iterate through the associative array using foreach:
foreach (array as $value) {
Use $value as the value of the current element
}
foreach (array as $key = = $value) {
Use $key as the subscript for the current element
Use $value as the value of the current element
}
If you want to modify the value of an array element during traversal, you can use & to make a reference value:
foreach ($arr as & $value) {
$value = $value * 2;
}
3) Array Pointers:
Each array has a pointer (a locator), which, by default, points to the first element of the array. If the array is traversed, then the pointer is in the element that the pointer is currently pointing to
Then, move backwards, pointing to the address of the next element. After traversing the last element, the pointer continues to move down, moving out of the current array space and again accessing the group elements
, the return is false.
4) Use the function each () for array traversal:
Each (): Gets the element that the current array pointer points to from the array, returns an array with four elements (Association + index), and moves the array's pointer down one
The list is a syntax structure that will get the elements in the array that must be indexed at 0, and the corresponding assignment to the variable specified by the list.
Syntax: List ($name, $age, $gender) = Array (' DQRCSC ', 24, ' Male ');
Equivalent to $name = ' DQRCSC '; $age = 24; $gender = ' Male ';
Because each of the returned results has two index elements 0 and 1, it is common to use list to pair with each
5) Common functions for arrays:
Key (): Gets the index of the element pointed to by the current pointer of the array
Current (): Gets the value of the element that the array pointer points to
Next (): Returns the value of the next element pointed to by the current pointer of the array, and moves the pointer down one
Prev (): Returns the value of the previous element where the current pointer is located, and moves the pointer forward one
Note: If the current pointer is already in the last element of the array element, then next returns false, the pointer still moves down one bit, and the array is moved out, and the current pointer is already in the
The first element of an array element, then using prev will move the array's pointer over the group and return false. Next () and Prev () are unable to pull the pointer of the group
The position of the law.
The following two functions can be used to reset an array pointer:
Reset (): Resets the pointer to the array so that the array pointer points to the first element, and if the value of the first element is successfully returned, the failure returns false
End (): Gets the value of the last element of the array element, and the pointer points to the last element
Note: If the first element of the array is deleted, then after the pointer is reset, it is the second element (starting with the first valid element).
Array_keys (): Gets all the subscript of an array, returns an indexed array, and the value of the array element is the subscript of the target array
Array_values (): Gets all the values of the array, returns an indexed array
Array_push (): Add an element from the end of the array (simulate into the stack)
Array_pop: POPs an element from the end of the array (simulates the stack)
Array_shift: Moving array elements out of the array header (simulating the team)
Array_unshift: Inserting data from the head of an array (simulating a queue)
Conversion between an array and a string:
Explode (): breaks a string into a series of small strings according to the specified delimiter, becoming an element of the array
Implode (): concatenation of all elements of an array into a character string
Range (): a function that automatically generates an array element according to the ASCII code to specify the range
Array_rand (): Randomly removes the specified number of element subscripts from the array
Shuffle (): The elements in the original array are scrambled
Merging of arrays: + and Array_merge ()
+: Saves all elements of the first array, and if the elements of the subsequent array are the same as the subscript of the elements of the preceding array, then it is ignored, if the difference is added to the first array,
Eventually returns an array
Array_merge (): Preserves all elements of the first array, and if the elements of the subsequent array are the same as the elements of the preceding array, overwrite the previous array's elements with the
If it is an index subscript, it is added directly after the first array, resetting the index.
The array in PHP learning note 7--php