This article will introduce you to a simple php entry-level instance. It mainly describes the operations and functions of php array values and the instances of array traversal and array sorting, for more information, see.
Array value operations
1. Value Extraction
In PHP, use list to extract the values in the array, such as list ($ a, $ B) = $ array. If the number of values in the list is greater than the number of arrays, the excess values in the list are set to NULL. You can also use commas to skip the values in the array, such as list ($ a, $ B) = $ array.
2. Divide Arrays
To obtain the sub-array, use array_slice (array, offset, length. It returns a new subscript array starting from 0. If the subscript of the original array is a string, it seems meaningless. It is best not to use it. You can use array_splice to obtain the substring.
3. Divide the array into multiple arrays
Array_chunk can be used to divide an array into two-dimensional arrays. For details, refer to the official instructions at the link.
4. Keys and values
Array_keys ($ array) to obtain an array composed of array Indexes
Array_value ($ array) is used to obtain an array consisting of array values. The index is reassigned from 0.
Array_key_exists ($ key, array). Check whether the element exists.
Array_splice: Delete the inserted element.
5. Conversion between arrays and variables
Extract (array) converts an array to a variable.
Compact () converts a variable to an array
6. Search for Arrays
In_array (array,) returns whether the element exists in the array.
Array_search () returns the index of the element to be found.
7. The entire Array Function
Array _ sum () calculates the sum of arrays.
Array _ merge () combines two arrays.
The values of array _ diff () are different.
Array _ filter () filter element
8. Set, stack, and queue
Array _ unique () is a collection of two arrays. If the values are the same, the indexes of the previous array are retained.
Array _ intersect () obtains the intersection of two numbers and retains the index of the first array.
Array _ push () is added to the stack.
Array _ pop () pops up the stack.
Array _ shift () is added to the queue.
The array _ unshift () pop-up queue.
1. Simple Traversal
In PHP, the simplest way to traverse arrays is for and foreach. Foreach can be written in two ways: one is to traverse only the value and the other is to traverse the index and value. For details, see the following code.
The Code is as follows: |
Copy code |
$ Test01 = array ('A', 'B', 'C '); // For ($ I = 0; $ I & lt; count ($ test01); $ I ++ ){ Echo $ test01 [$ I]; } // Foreach value only Foreach ($ test01 as $ value ){ Echo $ value; } // Foreach key and value $ Test01 = array ('A' = & gt; 'aaa', 'B' = & gt; 'bbbbb', 'c' = & gt; 'cccccc '); Foreach ($ test01 as $ key = & gt; $ value ){ Echo "$ key = & gt; $ value "; }
|
2. iterator Traversal
In PHP, iterative traversal mainly uses the following functions.
The current element of current () iteration.
Reset () moves to the first element and returns it.
Next () moves to the next element and returns it.
Move prev () to the previous element and return it.
End () moves to the last element and returns it.
Each () returns the index and value of the current element in the form of an array and moves it to the next iteration.
Key () returns the current index.
Array _ walk () calls a function for each element.
Array _ reduce () is calculated in sequence for each element.
The Code is as follows: |
Copy code |
$ Test01 = array ('A' = & gt; 'aaa', 'B' = & gt; 'bbbbb', 'c' = & gt; 'cccccc '); While (list ($ key, $ value) = each ($ test01 )){ Echo "$ key = & gt; $ value". "n "; } Array_walk ($ test01, pai_test ); Function performance_test ($ key, $ value ){ Echo "walk: $ key = & gt; $ value". "n "; }
$ Test02 = array (1, 2, 3, 4, 5 ); Echo array_reduce ($ test02, performance_test ); Function performance_test ($ run_result, $ current_value ){ Return $ run_result + $ current_value * $ current_value; } |
3. Sort Arrays
In PHP, there are three sorting methods: Index sorting, value sorting (the original index is not retained), and value sorting (the original index is retained ). Each function is divided into three types: ascending, descending, and user-defined order. They are as follows:
Sort by index: ① ascending ksort () ② descending krsort () ③ User-Defined ordered uksort ()
Sorting without retaining original index values: ① ascending sort () ② descending rsort () ③ user-defined order usort ()
Sort the original index values: ① asort () ② arsort () ③ user-defined order uasort ()
In PHP, you can also use array_multisort to sort multiple arrays at a time, but the project may be used less.
Flip the array, flip the numeric index, and the index starts from 0 again: array_reverse ()
Change the index and value: array_flip ()
Random order: shuffle ()