Home Backend Development PHP Tutorial PHP Array function classification

PHP Array function classification

Aug 08, 2016 am 09:25 AM
apple arr array function nbsp

1. Basic functions of array operations
1. Array key names and values ​​
array_values($arr); Get the key name of the array
array_flip($arr);                                                                                                                                                                                                                  The value and the key name in the array can be exchanged (if there are duplicates, the former will be overwritten by the latter)
in_array("apple",$arr); ("apple",$arr); Retrieve apple in the array and return the key name if it exists
array_key_exists("apple",$arr); Retrieve whether the given key name exists in the array
isset($arr [apple]): Retrieve whether the given key name exists in the array
2, the internal pointer of the array
current($arr); Return the current unit in the array
pos($arr); Return the array The current unit yKey ($ ARR); the key name of the current unit in the array ($ Arr); pour the internal pointer in the array back to a
next ($ arr); it will be Move the internal pointer in the array forward one bit
end($arr); Point the internal pointer in the array to the last unit
reset($arr; Point the internal pointer in the array to the first unit
each($arr); will return a constructed array of key names/values ​​of the current element of the array, and move the array pointer forward by one
list($key,$value)=each($arr); Obtain The key name and value of the current element of the array
3. Conversion between arrays and variables
extract($arr); Used to convert the elements in the array into variables and import them into the current file. The key name is used as the variable name. , value as variable value
compact(var1,var2,var3); Create an array with the given variable name
2. Segmentation and filling of the array

1. Segmentation of the array
array_slice($arr,0,3); You can remove a segment from the array. This function ignores the key name
array_splice($arr,0,3, array("black","maroon")); You can remove Take out a section of the array. The difference from the previous function is that the returned sequence is deleted from the original array. 2. Split multiple arrays.
array_chunk($arr,3,TRUE); You can split an array into multiple arrays. , TRUE is to retain the key name of the original array
3. Array filling
array_pad($arr,5,'x'); Fill an array to the specified length
3. Arrays and stacks

array_push($arr,"apple","pear"); Push one or more elements to the end of the array stack (push) and return the number of elements pushed onto the stack
array_pop($arr); Will The last element of the array stack is popped (popped off the stack)
4. Arrays and queues
array_shift($arr); the first element in the array is moved out and returned as the result (the length of the array is reduced by 1, and the other elements are Move forward one position, the numeric key name is changed to zero technology, and the text key name remains unchanged)
array_unshift($arr,"a",array(1,2)); Insert one or more at the beginning of the array Element
5. Callback function
array_walk($arr,'function','words'); Use user function to process each member of the array (the third parameter is passed to the callback function function)
array_map("function",$arr1,$arr2); Can handle multiple arrays (when using two or more arrays, their lengths should be the same)
array_filter($arr,"function") ; Use the callback function to filter each element in the array. If the callback function is TRUE, the current element of the array will be included in the returned result array, and the key name of the array remains unchangedarray_reduce($arr,"function" ,"*"); Convert to a single-valued function (* is the first value of the array)
6. Sorting of arrays

1. Sort the array by element value
sort($arr) ; Sorting from small to large (the second parameter is the sorting method), ignoring the array sorting of key namesrsort($arr); Sort from large to small (the second parameter is how to sort), ignore the array sorting of key names
usort($arr,"function"); Use user-defined comparison The function sorts the values ​​in the array (there are two parameters in the function, 0 means equal, a positive number means the first is greater than the second, a negative number means the first is less than the second) Array sorting ignoring key names
asort($arr); Sort from small to large (the second parameter is how to sort) Array sorting preserving key names
arsort($arr); Sort from large to small (the second The parameter is how to sort) Array sorting that preserves key names
uasort($arr, "function"); Use a user-defined comparison function to sort the values ​​in the array (there are two parameters in the function, 0 means Equality, a positive number means the first one is greater than the second one, a negative number means the first one is less than the second one) Array sorting that preserves key names
2. Sort the array by key name
ksort($arr); Follow Sort the key names in positive order
krsort($arr); Sort the key names in reverse order
uksort($arr, "function"); Use the user-defined comparison function to sort the key names in the array (in function There are two parameters, 0 means equal, a positive number means the first one is greater than the second one, a negative number means the first one is less than the second one)
3. Sorting by natural sorting
natsort($arr); Natural sorting (Ignore key names)
natcasesort($arr); Natural sorting (ignore case, ignore key names)
7. Array calculations
1. Sum of array elements
array_sum( $arr); Perform a sum operation on all elements inside the array
2. Merge of arrays
array_merge($arr1,$arr2); Merge two or more arrays (same string key name, followed by Overwrite the previous one, the same numeric key name, the latter one will not be overwritten, but appended to the back) "+" $arr1+$arr2; For the same key name, only the last one is retained
array_merge_recursive($arr1, $arr2); Recursive merge operation, if there are the same string key names in the array, these values ​​will be merged into one array. If a value itself is an array, it will be merged into another array according to the corresponding key name. When the arrays have the same array key name, the latter value will not overwrite the original value, but will be appended to the back
3. Difference set of the array
array_diff($arr1,$arr2); Return the difference set Result array
array_diff_assoc($arr1,$arr2,$arr3); Returns the difference result array, and the key names are also compared
4. Intersection of arrays
array_intersect($arr1,$arr2); Returns the intersection Result array
array_intersect_assoc($arr1,$arr2); Returns the intersection result array, and the key names are also compared
8. Other array functions
range(0,12); Create an array containing the specified range Array of cells
array_unique($arr); Remove duplicate values ​​​​in the array, and the original key names will be retained in the new array
array_reverse($arr,TRUE); Return a unit order that is opposite to the original array Array, if the second parameter is TRUE, keep the original key name
//srand((float)microtime()*10000000); Random seed trigger
array_rand($arr,2); Randomly from the array Take out one or more elements
shuffle($arr); shuffle the order of the array

The above has introduced the classification of PHP Array functions, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Update | Hacker explains how to install Epic Games Store and Fortnite on iPad outside the EU Update | Hacker explains how to install Epic Games Store and Fortnite on iPad outside the EU Aug 18, 2024 am 06:34 AM

Update: Saunders Tech has uploaded a tutorial to his YouTube channel (video embedded below) explaining how to install Fortnite and the Epic Games Store on an iPad outside the EU. However, not only does the process require specific beta versions of iO

Apple\'s \'HomeAccessory\' device revealed to have an A18 chipset among other features Apple\'s \'HomeAccessory\' device revealed to have an A18 chipset among other features Sep 27, 2024 am 09:02 AM

Fresh details of Apple's HomePod-like device with an integrated screen have surfaced and they paint a clearer picture of the device which has been referred to as 'HomeAccessory'. When it launches, it will be Apple's answer to Google's Nest Hub Max an

iPhone 16 Pro and iPhone 16 Pro Max official with new cameras, A18 Pro SoC and larger screens iPhone 16 Pro and iPhone 16 Pro Max official with new cameras, A18 Pro SoC and larger screens Sep 10, 2024 am 06:50 AM

Apple has finally lifted the covers off its new high-end iPhone models. The iPhone 16 Pro and iPhone 16 Pro Max now come with larger screens compared to their last-gen counterparts (6.3-in on the Pro, 6.9-in on Pro Max). They get an enhanced Apple A1

Apple iPhone 16 and iPhone 16 Plus launched with 48MP \'Fusion camera\', Camera Control and A18 chip Apple iPhone 16 and iPhone 16 Plus launched with 48MP \'Fusion camera\', Camera Control and A18 chip Sep 10, 2024 am 09:30 AM

Apple has officially announced the iPhone 16 and iPhone 16 Plus, introducing key hardware updates with the new A18 chip. Both models come in two sizes—6.1 inches and 6.7 inches—with Super Retina XDR displays. They also feature aluminum designs and ar

Apple analyst offers new insights into upcoming iPhone 16, iPhone SE 4 and even rumoured iPhone 17 Air releases Apple analyst offers new insights into upcoming iPhone 16, iPhone SE 4 and even rumoured iPhone 17 Air releases Aug 12, 2024 pm 10:01 PM

Apple's next-generation iPhones are right around the corner. While the company has not shared the date of a new launch event yet, all signs appear to point to a September release, just like last year's iPhone 15 series. Incidentally, footage of dummy

New Apple iPhone 16 design and colours shown in comparison video against iPhone 15 New Apple iPhone 16 design and colours shown in comparison video against iPhone 15 Aug 12, 2024 am 06:59 AM

The launches of Apple's next iPhones are not expected for at least another month. Nonetheless, footage showing dummy iPhone 16 units in what are said to be official launch colours continues to emerge online. Incidentally, while Google previously rele

Analyst discusses launch pricing for rumoured Meta Quest 3S VR headset Analyst discusses launch pricing for rumoured Meta Quest 3S VR headset Aug 27, 2024 pm 09:35 PM

Over a year has now passed from Meta's initial release of the Quest 3 (curr. $499.99 on Amazon). Since then, Apple has shipped the considerably more expensive Vision Pro, while Byte Dance has now unveiled the Pico 4 Ultra in China. However, there is

iOS 18 beta now supports Adaptive Lighting on Matter smart bulbs iOS 18 beta now supports Adaptive Lighting on Matter smart bulbs Aug 14, 2024 pm 12:48 PM

In addition to the generative AI features plannedfor iOS 18, several Reddit users have discovered that the iOS 18.1 beta enables Adaptive Lighting by default for Nanoleaf’s Matter-compatible smart bulbs. Adaptive Lightning, a staple of Apple Home sin

See all articles