Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common array functions of PHP and how to write their code

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What are the common array functions of PHP and how to write their code? in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

I. some basic operation functions of the array about key names and values

1. Get all the keys or values of the array: array_keys () array_values ()

$arr_keys = array_keys ($array); $arr_values = array_values ($arr)

two。 Swap the position of the keys and values in the array. If you repeat the previous one, it will be overwritten by the following: array_flip ()

$arr2 = array_flip ($arr)

3. Whether the given value is in the array: in_array (value,array)

$bool = in_array ('hello',$arr)

4. Search for a value in the array, return its key if it is not present, or FALSE:array_search () if it is absent

$bool = array_search ('hello',$arr)

5. Whether the given key exists in the array: isset (array [key]) and array_key_exists (key,array)

$bool = array_key_exists ('axiom Magazine Arr)

6. Get the number of array elements: count (array,mode). A mode of 1 means that the array is counted recursively. The default is 0. Alias sizeof ()

$n = count ($arr); / / equivalent to: $n = sizeof ($arr)

7. Change the key name in the array to either all lowercase or uppercase: array_change_key_case (array,case). Case has two common quantities: CASE_UPPER or CASE_LOWER (default), which is all lowercase by default

$lowarr = array_change_key_case ($arr,CASE_LOWER)

8. Count the number of occurrences of all values in the array: array_count_value (array). Returns an array where the key is the value of the original array and the value is the number of times the element appears in the original array

$arr_count = array_count_values ($arr)

9. Get the first or last key name of the array: array_key_first (array), array_key_last (array)

$key = array_key_first ($arr)

10. The last element of the pop-up array:

$last = array_pop ($array)

Press one or more cells into the end of the array or the beginning of the array and return the number of new arrays:

$new_array = array_push ($array,$value1,$value2,...); $new_array = array_unshift ($array,$value1,$value2,...)

11. Reverse the array: array_reverse (array)

$reverse = array_reverse ($arr)

twelve。 Summation or product of all values in an array:

$sum = array_sum ($array); $product = array_product ($array)

13. Remove duplicate values from the array:

Array_unique ($array,SORT_STRING); the sort_falgs parameter is used to modify the sorting behavior: SORT_NUMERIC-compare in numeric form, SORT_STRING-compare in string form

14. Scramble array: shuffle (array)

$bool = shuffle ($arr)

15. Randomly takes one or more key names from the array: array_rand (array,num=1), and returns an array containing random key names.

Summary of some operation functions about creation and segmentation of the array

1. Split an array into multiple arrays: array_chunk (array,size,preserve_keys)

Parameters:

Size: indicates the number of elements in each array

Preserve_keys: indicates whether to keep the original key name. The default is false.

Function returns a two-dimensional array

$myarr = array_chunk ($arr,2)

two。 Create an array with the value of one array as its key name and the value of another array as its value: array_combine (keys,values)

$arr_1 = ['axiajiajiao Bengjiaojiao C']; $arr_2 = [' AANGHANGHANGHANGHANGHANGHANGZHANG]; $arr_3 = array_combine ($arr_1,$arr_2)

3. Populates the array with the specified key and value: array_fill_keys (keys,value)

$keys = array ('foo', 5,10,' bar'); $a = array_fill_keys ($keys, 'banana')

4. Populate the array with the given value: array_fill (start_index,num,value)

Parameters:

Start_index: the first index of the array

Num: the number of elements inserted, that is, the length of the array, must be non-negative

Value: the value to populate

$arr = array_fill (0pc10myname`)

5. Merge one or more arrays: array_merge (array1,array2...)

If the key name is the same, the character key name will be overwritten, and the numeric key name will not be overwritten, but will be appended to the back.

$a = array_merge ($arr_1,$arr_2)

6. Recursively merging one or more arrays: array_merge_recursive (array_1,array_2,...)

If the array has the same array key name, the latter value will not overwrite the original value, but will be appended to it.

7. Populates the array with a value of the specified length: array_pad (array,size,value)

Parameters:

Size: the length of the filled array, regular fill to the right side of the array, negative fill to the left side of the array

Value: the value to populate

8. Take a segment from the array: array_slice (array,offset,length,preserve_keys)

Parameters:

Offset: the offset at the beginning, either positive or negative

Length: the length obtained. A positive number indicates the number of elements obtained, and a negative number represents the distance from the end of the array.

Preserve_keys: whether to keep the original key name

10. Remove a part of the array and replace it with another value: array_splice (array,offset,length,replacement_array)

Parameters:

Replacement_array: removed cells are replaced by cells in this array

11. Use variables to create an array: compact (var1,var2,...), with the variable name as the key name and the variable value as the value of the element

twelve。 Export the variable from the array: extract (array), the key name is the variable name, and the value is the value of the variable

13. Assign the value of the array to the variable: list (var1,var2,...)

List ($drink, $power) = array ('coffee',' brown', 'caffeine')

14. Creates an array based on the range, containing the specified element: range (start,end,step)

Range (0pje 8jue 2) = > [0je 2je 4je 6je 8]

3. The basic function of array sorting is called sort

Other extensions can be added: r for reverse sorting, k for sorting key names, a for maintaining index relations, and u for comparing with custom functions. Introduce the situation of sort function in detail, other functions are similar

/ * bool sort (array & $array [, int $sort_flags = SORT_REGULAR]) the SORT_REGULAR parameter can change the sorting behavior with the following values: SORT_REGULAR-normal comparison unit (no change type) SORT_NUMERIC-unit is compared as a number SORT_STRING-unit is compared as a string SORT_LOCALE_STRING-unit is compared as a string according to the current locale setting, it can be changed with setlocale (). SORT_NATURAL-and natsort () sort strings in "natural order" similar to each unit. New in PHP 5.4.0. SORT_FLAG_CASE-can be merged with SORT_STRING or SORT_NATURAL (OR bit operation), sorting strings without case sensitivity. , /

The sort functions are roughly classified as follows:

2.sort (), rsort (): sort values in ascending and descending order 3.ksort (), krsort (): sort key names in ascending and descending order 4.asort (), arsort (): sort values in ascending and descending order while maintaining index relations, 5.usort (), uksort (), uasort (): use custom sort functions Sort by value in ascending order, key name in ascending order, and index relations in ascending order 6.natsort (): sort the array using the natural sort algorithm 7.natcasesort (): sort the array insensitive to case letters using the natural sort algorithm

IV. Array operation

The calculation of array difference u represents a custom callback function, diff represents a difference with data, and assoc represents a difference with an index

1. Calculate the difference of the array: array_diff (array1,array2,...) Compare array1 with other arrays and return values that are in array1 but not in other arrays. Returns an array, but the key name is not reserved

two。 Use the callback function to compare the data to calculate the difference of the array: array_udiff (arr1,arr2,...,value_cpmpare_func)

Use user-defined functions for data comparison instead of built-in functions.

3. Use key name comparison to calculate the difference of an array: array_diff_key (array1,array2,...)

Use key names instead of values for subtraction calculation

4. Use the callback function to compare key names to calculate the difference of the array: array_diff_ukey (arr1,arr2,...,key_compare_func)

5. Check the difference of the computed array with index: array_diff_assoc (array1,array2,..)

Use key name and value to calculate subtraction at the same time

6. Check the difference of the calculated array with the index, and compare the index with the callback function: array_diff_uassoc (arr1,arr2,...,key_compare_func)

Key_compare_func: a user-defined function for comparing key names.

7. Check the difference of the computed array with the index and compare the data with the callback function: array_udiff_assoc (arr1,arr2,...,value_cpmpare_func)

Value_cpmpare_func: a user-defined function for comparing data

8. Compare the data and the index with the callback function to calculate the difference of the array: array_udiff_uassoc (arr1,arr2,...,value_cpmpare_func,key_compare_func)

The calculation of array intersection is the same as the difference set, there are 8 functions:

Array_intersect () uses data for intersection comparison array_uintersect () uses data for intersection comparison, but custom function comparison array_intersect_key () uses key name for intersection comparison array_intersect_ukey () uses key name for intersection comparison But the custom function compares array_intersect_assoc () with both the data and the key name array_intersect_uassoc (), but the key name uses the custom function to compare array_uintersect_assoc () with both data and key name, but the data uses the custom function to compare array_uintersect_uassoc () with both data and key name All use custom functions to share the answers to questions about what PHP common array functions have and how to write their code. I hope the above can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report