Home > Article > Backend Development > What does it mean to traverse an array in php
PHP traversing the array means accessing each element in the array through the PHP code. The method of traversing the array: 1. Traverse through the for loop; 2. Traverse the array through the foreach loop, etc.
The operating environment of this article: Windows 7 system, PHP version 7.4, DELL G3 computer
What does php mean by traversing an array?
PHP traversing an array means accessing every element in the array through PHP code.
Array creation:
1.array(): Generate an array
$a=array("Dog","Cat","Horse"); print_r($a);
Array value or, key => value an array variable
Strongly typed language: 1. Only data of the same data type can be stored in the array. 2. The length of the array is fixed. 3. It is continuous in the memory.
2. Associative array: $arr=array("111 "=>"ddd","daf"=>"ddd")
key and value exist in pairs
3.unset() deletes array elements;
$a=array("Dog","Cat","Horse"); unset($a[0]);
4. Add elements:
$a[0]=10;Add elements
$a[]=10;Append elements
array_push($a ,23);Append elements
Traverse the array:
1.for($i=0;$ib4e42ed07684b6a21b1c02955bf84dad";
}
Can only traverse the index array
2.foreach loops through the array
foreach($a as $v) { echo $v."<br>"; } foreach($a as $v=>$k) { echo $v."--"."$k"."<br>"; }
Key and value are traversed simultaneously
3. List() and each() are combined to traverse
var_dump(each($a)); take out the element pointed to by the current pointer , return the new array and adjust the pointer by one
list($b,$c,$d,$e)=$a; echo $b;
Give the array on the right to a set of variables
list($k,$v)=each($a); while(list($k,$v)=each($a)) { echo $k."--"."$v"."<br>" }
4 Array pointer operation:
key(): return The internal pointer of the array currently points to the key name of the element
current(): Returns the current element (unit) in the array.
next(): Moves the pointer pointing to the current element to the next element position, and return the value of the current element
prev(): Move the pointer to the current element to the position of the previous element, and return the value of the current element
end(): Move The internal pointer of the array points to the last element and returns the value of the element
reset(): Points the internal pointer of the array to the first element and returns the value of this element
do { current($a) } while(next($a));
Execute first Then judge the conditions
in_array()判断元素是否在数组中出现 array_reverse()翻转数组 count()数组的无素个数 array_unique()删除数组中的重复值并返回新的数组; unset()删除数组 的某个值 array_values()重新索引 array_merge()合并数组;
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does it mean to traverse an array in php. For more information, please follow other related articles on the PHP Chinese website!