First, define the array
(1) Indexed array
$arr =array (1,2,3,3);
(2) Associative arrays are similar to collections
$arr 1=array ("One" = "111", "one" and "222");
(3) An assignment definition array:
$arr 2["One"]= "AAAA";
Note:
Three characteristics of a strongly typed language array:
1, the array can only store the same type of data
2, the length of the array is fixed
3, in memory is continuous
PHP is a weakly typed language
Second, array operation
1. Array values
(1) Var_dump ($arr 1);
(2) $arr 1["the other"];
2. Deleting elements
unset ($arr [0]);
3. Adding elements
$arr 2[key]=value;
4. Append elements (added after the last array)
(1) $arr 2[]=1;
(2) Array_push ($arr, 12);
Third, iterating over the array (the second important)
1. For loop can only traverse indexed array
$arr =array (1,2,3,3);
for ($i =0; $i <count ($arr); $i + +) {
echo $arr [$i]. " <br/> ";
}
$arr 1=array ("One" = "111", "one" and "222");
2, the first loop of the Foreach loop to give 1 to V, the second time 2 to V can traverse the index and associative array
/* $arr =array (1,2,3,3);
foreach ($arr as $v)
{
echo $v. " <br/> ";
}
foreach ($arr as $k = $v)
{
echo $k. " ----". $v." <br/> ";
}
Note:
(1) Strongly typed language foreach (int i in arr) {}
(2) JS for (Var A in arr) {} A takes out the key
3. The list () function and each () function combine traversal
(1) $arr =array (1,2,3,3);
Var_dump (each ($arr));
Takes out the element pointed to by the current pointer, returns a new array containing key and value, and moves the pointer one backward.
(2) List ($a, $b, $c, $d) = $arr; Give the elements of the right array to a set of variables
Echo $c;
$arr =array (1,2,3,3);
while (list ($k, $v) =each ($arr)) {
echo "{$k}={$v}<br/>";
}
4. Pointer traversal
Next ($arr); Pick the pointer back one
Prev ($arr); Pick the pointer forward one
Reset ($arr);//Reset pointer
End ($arr);//The pointer reaches the last
Echo current ($arr); Takes the value of the element pointed to by the current pointer
echo Key ($arr)//index of the element to which the current pointer is pointing
Do
{current ($arr)//Perform loop body after-walk condition first
}
While{next ($arr)
}
Note: While loop: The first number is not output by the loop body while the loop is in the first condition, so there is not a while loop.
Eg:
while (list ($k, $v) =each ($arr)) {
echo "{$k}={$v}<br/>";
}
Reset ($arr); Do not add this output 5, or 10
while (list ($k, $v) =each ($arr)) {
echo "{$k}={$v}<br/>";
}
Four or five as follows:
PHP array--2017-04-16