PHP Array:
Array Basics:
In ①php, the subscript of an array can be an integer, or a string.
in ②php, the order of elements in an array is determined not by the subscript, but by the order in which they "join".
Definition: $arr 1=array (element 1, Element 2 ...);
Cases:
Array (1, 5, 1.1, "ABC", True, false); you can store any data at this time as "default subscript",
Array (2=>1, 5=>5, 3=>1.1, 7=> "abc", 0=>true);//subscript can be set arbitrarily (no order, no continuous)
Array (2=>1, 5, 1=>1.1, "abc", 0=>true)// can be added subscript, also can not add (default subscript), the subscript is:2,3,1,4,0
default subscript rule: The maximum number subscript that has been used before +1
Array (2=>1, ' DD ' =>5, 1=>1.1, "abc", 0=>true) mixed subscript, also follows the default subscript rule
Array ( -2=>1, ' DD ' =>5, 1.1, "ABC", True); The negative subscript is not counted in the integer subscript, but only as a character Poute the last 3 The subscript for the item is: 0, 1, 2
Array (2.7=>1, ' DD ' =>5, 1=>1.1, "abc", 0=>true) , floating-point index is automatically converted to an integer, and the decimal is erased directly
Array ("2.7" =>1, ' DD ' =>5, "one" =>1.1, "abc", true) a purely numeric string subscript, treated as a number , then subscript as:2, ' DD ', 11, 12, 13
Array (2=>1, ' DD ' =>5, true=>1.1, "abc", False=>true) boolean current subscript, true is 1 c4>,false is 0;
Array (2=>1, ' DD ' =>5, 2=>1.1, "abc", true) if the subscript follows the previous repetition, it simply overrides the value of the previous name, which is equivalent to:Array (2 =>1.1, ' dd ' =>5, ' abc ', True)
Array classification:
1, key value relationship:
① associative array: usually refers to an array that is labeled as a string, and that string can roughly express the meaning of the data.
$person = Array (
"Name" = "Floret",
"Age" =>18,
"Edu" = "University Graduate" ,
);
② index array: with JS;
2. By array level:
① one-dimensional arrays:
$arr 1 = Array (
"Name" = "Floret",
"Age" =>18,
"Edu" = "University Graduate" ,
);
② Two-dimensional arrays:
$arr 1 = Array (
"Name" = = Array (' Floret ', ' small Fang ', ' Xiao Ming ',);
"Age" = = Array (18, 22, 19),
"edu" = = Array ("University graduate", ' Secondary school ', ' Primary school ')
);
③ Multidimensional Arrays:
Subscript form: $v 1 = array name [ subscript ] [subscript] [ ...]
Array Traversal:
A foreach ($arr as [$key = =] $value)//$key can be called a key variable,$value can be called a value variable.
{
here, you can $key and the $value do all the possible things-because they are a variable
$key represents every time an index of an element is obtained, possibly a number, or it can be a string
$value represents the value of the element each time it is acquired, possibly various types.
This loop structure iterates through the first item of the array, loops to the last item, and then ends.
}
Array pointers and traversal principles:
each array has a "pointer" inside it that determines the element that is taken when the array is currently being evaluated.
The foreach traversal process is dependent on the pointer!
The pointer is in addition to the position setting for the Foreach loop, and some other functions also depend on the pointer:
1,$v 1 = current ($arr 1); Get $arr 1 The value of the element to which the current pointer is pointing, or if no element is pointed to ; false
2,$v 1 = key($arr 1) , get the subscript of the element pointed to by the current pointer in $arr 1 .... false
3,$v 1 = next ($arr 1) , move the pointer to the next element, and then take the value of the next element;
4,$v 1 = prev ($arr 1); Move the pointer to the previous element and get the value of the previous element
5,$v 1 = reset ($arr 1); Move the pointer to "first element" and get the value of the element--array pointer initialization
6,$v 1 = end ($arr 1); Move the pointer to "last element" and get the value of the element
7,$v 1 = each ($arr 1) , gets the subscript and value of the current element, and then moves the pointer to the next position.
foreach can have operations such as break and continue; Value variables are passed by default, and value variables can be manually set as reference passes during traversal:
foreach ($arr as $key = & $value) {...}
Key variable cannot be set as a reference pass
$arr 2=array (5,15,3,4);
foreach ($arr as $key = = $value) {
$value *=2;
echo "<br> $key + $value"
}
echo "<br>";
Print_r ($arr 2);
The value at this time is:0=>10,1=>30,2=>6,3=>8;
But the array output is: Array ([0]=>5 [1]=>15] [2]=>3 [3]=>4];
$arr 2=array (5,15,3,4);
foreach ($arr as $key =&$value) {
$value *=2;
echo "<br> $key + $value"
}
echo "<br>";
Print_r ($arr 2);
The value at this time is:0=>10,1=>30,2=>6,3=>8;
But the array output is: Array ([0]=>10 [1]=>30] [2]=>6 [3]=>8];
Bubble Sort:
Target: Arrange the following arrays in a positive order (small to large)
$arr 2 = Array (5, 3, 4, 9, one );
$arr 2=array (5,15,3,4,9,11);
$len =count ($arr 2); A n-1 "bubbling" comparison process is required
echo "<br> before sorting:";
Print_r ($arr 2);
for ($i =0; $i < $len -1;++ $i) {Sets the comparison count by 1 less than the previous number, and the first comparison needs to compare n-1 times
for ($k =0; $k < $len-$i; + + $k) {Sets the number of times this need to be compared, where the $k and $k+1 are compared
if ($arr 2[$k]> $arr 2[$k +1]) {
$temp = $arr 2[$k];
$arr 2[$k]= $arr 2[$k +1];
$arr 2[$k +1]= $temp;
}
}
}
echo "<br> after sorting:";
Print_r ($arr 2);
2018-1-25 Array