Learned the knowledge of arrays today.
One, array 1. Definition of arrays
What is an array? An array is actually a set of data, and we can extract the data we want based on the subscript value (that is, the key value relationship) in the array. There are usually two ways to define an array:
Method 1: $arr = Array (10,20,30,40,50);
Method 2: $arr [] = 10;
$arr [] = 11;
$arr [] = 12;
In this way, you can create an array and assign values to the array. The created array is similar to a small warehouse, and we can always go to the warehouse to find the data we want.
2. Classification of arrays
Arrays can be divided into three categories based on their subscripts (key-value relationships): enumerated numbers, associative arrays, and mixed arrays.
An enumerated number: Also known as an indexed array, whose primary subscript is a set of zero-based positive integers (similar to arrays in other languages). Like what:
$arr = Array (11,22,33,44,55,66,77);
The subscript of the above array is the zero-based positive integer followed by 0,1,2,3,4,5,6
Associative array: The subscript of an array is a string, which usually represents a certain meaning and makes it easier to fetch data. Like what:
$arr 1 = Array (
"Name" = "localhost",
"PSD" = 123456,
"Username" = "root"
);
This type of array is an associative array.
Mixed arrays: Arrays of both numeric subscripts and character subscripts, such as:
$arr = Array (
1=>1,
2=>2,
"User" = "very shape",
"PSD" =>123456
);
The above array is a mixed array.
Note: 1. If no index value is set in the array element, its subscript is the current maximum index of +1;
2. If the subscript is a floating-point number, it will be rounded down.
3. If there is a repetition, the following number overrides the previous number.
An array of 4.php, the last item of which a comma can have, does not represent a single item.
5. The order of the array cells is determined by the order in which they are placed in the array, not by their subscripts.
3. Multidimensional Arrays
A multidimensional array is actually an array of multiple arrays nested within each other, as follows:
$arr = Array (
Array (1,2,3,4,5,6,7,88,99),
"ABC" =>array (11,22,33,44,55),
2=> Array (55,66,77,88,99),
"Def" =>array ("AA" + "AA", "bb" = "BB", "CC" and "CC")
);
The above array is a common two-dimensional array;
4. Basic usage of arrays
1) The average of a one-dimensional array is obtained;
$a = array (1,2,3,4,5,6,7,8,9);
$len = count ($a); The length of this array
$sum = 0; For the sum
$c = 0; Number of stores used for storage
For ($i =0; $i < $len; + + $i) {
$sum + = $a [$i]; Accumulator
+ + $c; Counter
}
echo "Average is". ($sum/$c);
2) Find the average of a two-dimensional array
$AA = Array (//two-D arrays
Array (11,22,33),
Array (22,33,44),
Array (222,222,333,444
);
$len = count ($AA); Gets the length of the $AA
$sum = 0; For the sum
$c = 0; For counting
for ($i =0; $i < $len; + + $i) {//Loop first-level array
$t = $aa [$i]; Remove the number of groups I
$len 2 = count ($t); Get the length of the number of groups I
for ($k =0; $k < $len 2;++ $k) {//cyclic I-arrays
$sum + = $t [$k]; Loops all the items in all arrays
+ + $c; Count how many items are there
}
}
echo "average". ($sum/$c);
3) Find the maximum value of one-dimensional array
$arr = Array (10,52,85,32,1,5,4,632,5);
$max = $arr [0]; Put the first item in $max first, assuming the first item is the maximum
$pos = 0; Assuming that the first item is the largest, subscript is 0;
$len = count ($arr); Gets the length of the array
for ($i =0; $i < $len; + + $i) {//traversal array
if ($arr [$i]> $max) {//compares each item to the maximum value
$max = $arr [$i]; Assign a new maximum value to $max
$pos = $i; The subscript of the maximum value is paid to $pos
}
}
echo "Maximum value:". ($max);
The echo "Max subscript is". ($pos);
Ii. Methods of arrays 1. How the array is traversed
There are many ways to iterate over arrays, and first we look at the first
foreach ($ array variable name as "$key = =" $value) {
loop body; Here you can go to "use" $key and value;
}
Note :
1) $key and $value is the subscript and corresponding value of each cell (item) of the array that the traversal statement has acquired over and over. Moreover, it always takes the data sequentially from the beginning of the array.
2) foreach is also a normal looping syntax structure, and can have operations such as break and continue.
3) The default value-passing value of a variable during traversal is a value pass .
4) During traversal, value variables can be manually set to reference delivery :foreach ($arr as $key = & $value) {...}
5) foreach defaults to traversal on the original array. However, if some modification or some kind of pointer action is made to the array during the traversal, the arrays will continue to iterate over the copied array (the original array remains unchanged).
6) in foreach, if a value variable is a reference pass, it is done on the original array anyway.
The main principle of foreach is to use the direction of the internal pointer to traverse, by default, the pointer is to the first item, if the array does not specify the subscript, then the operation is through the pointer to the operation of the process, so-called traversal is to get the current cell key and value, and put the corresponding variable $ Key and $value, and then move the pointer to the next cell.
An array is a population of data, functions that can manipulate pointers are:
Current ($arr); Gets the value of the cell that contains the current pointer of the array
Key ($arr); Gets the value of the cell that contains the current pointer of the array
Next ($arr); First move the pointer of the array down (after) a cell, and then get the value of the new cell;
Prev ($arr); First, the pointer of the array is moved up (before) a cell, and then the value of the new cell is obtained;
End ($arr); First, the pointer of the array is moved to the last cell, then the value of the new cell is obtained;
Reset ($arr); First move the pointer of the array to the first cell, and then get the value of the new cell;
2. Use for and next to iterate through an array
In PHP arrays, it is often not possible to simply use a for loop to iterate, for a for loop can only loop an array labeled as a continuous, pure integer.
$arr = Array (1=>3, "a" =>7, ' BB ' =>65);
$len = count ($arr); Get length
for ($i =0; $i < $len; + + $i) {//loop array
$key = key ($arr); Gets the key of the current cell
$value = current ($arr); Gets the value of the current cell
echo "$key = $value"; Output key-value pairs
Next ($arr); Move the pointer down.
}
Use of 3.each functions
each () function: First take the subscript and value of the "current cell" of an array (and put in an array), and then move the pointer to the next cell.
$a = each ($ array name); A $ A is an array now.
Example:
$arr = Array (1=>3,4=>5, ' Nihao ' =>hh); Array $arr
$b = each ($arr); Gets the key value pair for the first item of the pointer, to $b
$c = each ($arr); Gets the key value pair for the second item of the pointer, to $c
Echo $b; Output $b result is: Array (0=>1,[key]=>1,1=>3,[value]=>3)
Echo $c; Output $c result is: Array (0=>4,[key]=>4,1=>5,[value]=>5)
Thus, the result of an array of each function variable is an array, which can be used to get the values of the keys in the original array based on the key values of the array.
The use of 4.list () functions;
The list () function is used to get the values of multiple cells in an array at a time, starting with a number from 0.
form of Use:
List ($ variable 1,$ variable 2, ...) ) =$ Array;
Note: The number of variables here corresponds to the array's number subscript unit, if the corresponding subscript of an array does not exist, it will be an error.
Example:
$a = array (0=>2,1=>3,5=>6,4=>6,3=44,2=>9);
List ($c, $d, $e, $f, $g) = $a; The value corresponding to the 0,1,2,3,4 in the $ A array is assigned to $c, $d, $e, $f, $g;
echo "c= $c, d= $d, e= $e, f= $f, g= $g"; Output: c=2,d=3,e=9,f=44,g=6;
With these principles, we can better expand: use while (), each (), list () to iterate over the array. Such as:
$arr = Array (1=>3, ' AA ' =>5,6=>7);
while (list ($key, $value) =each ($arr)) {
echo "$key = $value"
}
The combination of the While,each,list method above can be used to iterate the array well, and it acts like a foreach.
Iii. Ordering of arrays 1. Common sorting functions
2. Bubble Sorting Algorithm
Sort from small to large inside an array
$a = Array (5,10,8,7,55,6,2,5,8,79,4,1,66)
$len = count ($a);
for ($i = 0; $i < $len -1;++ $i) {//Total cycle $len-1 times required
for ($k = 0; $k < $len-$i-1) {//
if ($a [$k]> $a [$k +1]) {
$t = $a [$k];
$a [$k]= $a [$k +1];
$a [$k +1] = $t;
}
}
}
3. Select sort
Sort from small to large inside an array
$a = array (5,10,8,7,55,6,2,5,8,79,4,1,66);
$len = count ($a);
for ($i = 0; $k < $len -1;++ $i) {
$max = $a [0];
$pos = 0;
for ($k =0; $k < $len-$i; + + $k) {
if ($a [$k]> $max) {
$max = $a [$k];
$pos = $k;
}
}
$t = $a [$pos];
$a [pos] = $a [$len-$i-1];
$a [$len-$i-1] = $t;
}
It's here today.
PHP Learning day8---array knowledge