PHP learning notes array values and array traversal and sorting

Source: Internet
Author: User
Array Value operations 1 value extraction PHP, use list to extract the value in the array, such as list ($ a, $ B) $ array, if the value in the list is more than the number of arrays, the redundant values in the list are set to NULL,

Array Value operations

1. value extraction

In PHP, list is used to extract the values in the array, such as list ($ a, $ B) = $ array. if the values in the list are greater than the number of arrays, the extra 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 has two ways to write, one is to traverse only values and the other is to traverse indexes and values, for details, see the following code.

  1. $ Test01 = array ('A', 'B', 'C ');
  2. //
  3. For ($ I = 0; $ I <count ($ test01); $ I ++ ){
  4. Echo $ test01 [$ I];
  5. }
  6. // Foreach value only
  7. Foreach ($ test01 as $ value ){
  8. Echo $ value;
  9. }
  10. // Foreach key and value
  11. $ Test01 = array ('a' => 'AAA', 'B' => 'bbbbb', 'C' => 'cccc ');
  12. Foreach ($ test01 as $ key => $ value ){
  13. Echo "$ key => $ value ";
  14. }

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 instance code is as follows:

  1. $ Test01 = array ('a' => 'AAA', 'B' => 'bbbbb', 'C' => 'cccc ');
  2. While (list ($ key, $ value) = each ($ test01 )){
  3. Echo "$ key => $ value". "n ";
  4. }
  5. Array_walk ($ test01, pai_test );
  6. Function performance_test ($ key, $ value ){
  7. Echo "walk: $ key => $ value". "n ";
  8. }
  9. $ Test02 = array (1, 2, 3, 4, 5 );
  10. Echo array_reduce ($ test02, performance_test );
  11. Function performance_test ($ run_result, $ current_value ){
  12. Return $ run_result + $ current_value * $ current_value;
  13. }

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.