Array is a very powerful weapon in PHP. It is convenient and easy to use. Because it is extremely flexible to use, it can be used to realize linked lists, stacks, queues, heaps, so-called dictionaries, sets, etc. in data structures, and can also be converted. into XML format.
1. Use for
The for statement is not a good choice to traverse the array. It is generally not used and has too many limitations, because the subscripts of the array are often not continuous, or there are both integer subscripts and string subscripts, but there is such a If it happens to be an indexed array, and its subscripts are consecutive, then this is also a method.
$array = array('a', 'b', 'c', 'd', 'e');
$size = count($array); //Get the number of array units
for($i=0; $i<$size; $i++)
echo $array[$i].' ';
2. Use foreach
Foreach is more convenient and flexible than for. It is generally used. When using foreach($arr_name as $value), use the as keyword in the previous array to specify its elements. Of course, this is for a one-dimensional array. You can also get the key name of the element, as follows: foreach($arr_name as $key=>$value).
Each time the each function acts on the array, the pointer to the internal element moves backward by one unit. Each time each returns a fixed-format array of key/value pairs, specifically (1=>value, 'value'= >value, 0=>key, 'key'=>key). The next time each is used, it will move to the next element, example
The function of the list function is to assign it an array variable, and it will assign the elements with integers as key values in the array to its own parameters in order from small to large key values. If the parameters are not enough, the parameters will be filled. , if there are not enough values in the array, the parameter is assigned a null value, and the code continues
list($key, $val) = $lst;
echo ' ';
The values in the $lst array variable, the key values are integers are 1=>'a' in the front and 0=>'one in the back. The advantage of the list function is that even if the key value is small, the ranking is The subsequent elements will also be assigned to the front-to-back parameters in the list function in order from small to large.
Since each does not loop through the array, it only moves the pointer each time it is used, and the return value is false at the end of the array, so it is most appropriate to put it in a while
4. Use the array internal pointer movement function
The internal pointer of the array points to the first element in the array by default. The functions are roughly as follows: current(): returns the element value where the current pointer points to the position in the array; key(): returns the element key where the current pointer points to the position in the array; next(): moves the pointer to the next element position; prev(): moves the pointer to the previous element position; reset(): moves the array pointer to the position of the first element of the array; end(): moves the pointer to the first element position of the array The array pointer moves to the position of the last element of the array. The parameters they act on are the array variables themselves, and combined with do...while, they can achieve sequential and reverse order traversal of the array.
Copy code
echo 'key:'.key($arr).' current:'.current($arr).' '; //The current key and value point to the first element of the array by default
next($arr); //Move back one element and point to the second element
echo 'key:'.key($arr).' current:'.current($arr).' '; //Current key and value
next($arr); //Move back one more point to the third element
echo 'key:'.key($arr).' current:'.current($arr).' '; //Current key and value
prev($arr); //Move forward one, pointing to the second element
echo 'key:'.key($arr).' current:'.current($arr).' '; //Current key and value
end($arr); //Move to the last element of the array
echo 'key:'.key($arr).' current:'.current($arr).' '; //Current key and value
reset($arr); //Move to the first element of the array
echo 'key:'.key($arr).' current:'.current($arr).' '; //Current key and value
http://www.bkjia.com/PHPjc/893562.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/893562.htmlTechArticlePHP array traversal array is a very powerful weapon in PHP. It is convenient and easy to use. Extremely flexible, you can use it to implement linked lists, stacks, and queues in data structures...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.
Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.
The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values takes a relatively long time.
The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.
PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.
Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.
The PHP array merging and deduplication algorithm provides a parallel solution, dividing the original array into small blocks for parallel processing, and the main process merges the results of the blocks to deduplicate. Algorithmic steps: Split the original array into equally allocated small blocks. Process each block for deduplication in parallel. Merge block results and deduplicate again.
PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.