Introduction to PHP arrays
Array basics:
In php, the subscript of an array can be an integer or a string
In php, the order of the elements of the array is not determined by the subscript, but by the order of their "joining"
Definition:
$arr1 = array(element 1, element 2,...);
array(1,1.1,5,'abc',true,false); //Any data can be stored, at this time it is the default subscript
array(2=>1,4=>1.1,3=>5,7=>'abc',0=>true); //The subscripts can be set arbitrarily (no order required, No need to be consecutive)
array(2=>1,1.1,1=>5,'abc',0=>true); //You can add a subscript or not. If you don't add a subscript, it will be the default subscript. Mark
to
// The number of this number is: 2, 3, 1, 4, 0
array(2=>1,'dd'=>5,1=>1.1,'abc',0=>true); //Mixed subscripts, also follow the default subscript rules
array(-2=>1,'dd'=>5,1.1,'abc',true); //Negative subscripts are not included in the integer subscripts, but are only treated as character subscripts
// The last three sets of this array are: 0,1,2
array(2.7=>1,'dd'=>5,1=>1.1,'abc',true); //The floating point number subscript will be automatically converted to an integer, and the decimal part will be removed directly
array("2.7"=>1,'dd'=>5,"11"=>1.1,'abc',true); //Pure numeric string subscript, treated as a number
array(2=>1,'dd'=>5,true=>1.1,'abc',false=>true); //Boolean value is used as subscript, then true is 1, false is 0
array(2=>1,'dd'=>5,2=>1.1,'abc',true); //If the subscript is repeated with the previous one, it will simply overwrite the value of the previous subscript with the same name.
Other forms:
$arr1[] = 1;
$arr1[] = 5;
$arr1[] = 1.1;
… //Use [] directly after the variable to become an array, and assign values in sequence
$arr2['aa'] = 1;
$arr2['bb'] = 5;
$arr2[5] = 1.1;
… //The subscript written in this form is actually almost the same as using the array syntax structure
Classification of arrays:
From the key-value relationship, it is divided into:
Associative array: usually refers to an array whose subscript is a string, and the string can roughly express the meaning of the data
Example: $person = array("name" => "poe", "age" => 18, "edu" => "University graduate");
Index array: usually refers to an array whose subscript strictly starts from 0 and is a continuous numeric subscript - similar to js array
Divided from the array level:
One-dimensional array: Each element value in an array is an ordinary value (non-array value)
Example: $ Person = Array ("name" = & gt; "poe", "age" = & gt; 18, "edu" = & gt; "college graduation");
Two-dimensional array: Each item in an array is also a one-dimensional array.
$person = array(
"name" => array("xiaohua","xiaofang),
"age" => array(18,22),
"Edu" = & GT; Array ("University Graduation", "Primary School",)
);
Multi-dimensional array: and so on. . .
The general syntax form of multi-dimensional arrays:
$ v1 = number of array names [Nubble] [Laying] [......]
Array traversal:
Basic syntax for traversal:
foreach($arr as [$key =>] $value) {
//Here you can perform all possible operations on $key and $value -- because they are just a variable
//$key represents the subscript of each element obtained, which may be a number or a string
//$value represents the value of the element obtained each time, which may be of various types
//This loop structure will traverse from the first item of the array to the last item, and then end
}
Array pointers and traversal principles:
Each array has a "pointer" inside it, which determines the element obtained when the array currently takes value
During the foreach traversal process, it all relies on this pointer.
Example: $arr1 = array(2=>1,'dd'=>5,1=>1.1,'abc',0=>true);
wKioL1ZNx1Sxflt6AAAnrLtN90Q498.jpg
In addition to the position setting of the foreach loop, pointers also have some other functions that rely on pointers:
1: $v1 = current($arr1); //Get the value of the element pointed to by the current pointer in $arr1. If there is no element pointed to, it is false
2: $v1 = key($arr1); //Get the subscript of the element pointed to by the current pointer in $arr1. . . . . . . . . . . . .
3: $v1 = next($arr1); //Move the pointer to the "next element", and then get the value of the next element
4: $v1 = prev($arr1); //Move the pointer to the "previous element", and then get the value of the previous element
5: $v1 = reset($arr1); //Move the pointer to the "first element" and get the value of the element
6: $v1 = end($arr1); //Move the pointer to the "last element" and get the value of the element
7: $v1 = each($arr1); //Get the subscript and value of the current element, and then move the pointer to the next position
http://www.bkjia.com/PHPjc/1073352.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1073352.htmlTechArticlePHP array introduces the basics of arrays: In php, the subscript of an array can be an integer or a string. In php, The order of the elements of the array is not determined by the subscript, but by the order in which they are added...