PHP learning notes array values and array traversal and sorting

Source: Internet
Author: User
Tags arrays

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.