Common PHP array function _ PHP Tutorial

Source: Internet
Author: User
Tags random seed
Some common PHP array functions. Array skills sometimes solve many problems. You can get twice the result with familiarity with related functions. Are you familiar with the following array functions? I. familiarity with array operation basic function arrays sometimes solves many problems. You can get twice the result with familiarity with related functions. Are you familiar with the following array functions?

I. basic functions of array operations

Key name and value of the array

  • Array_values ($ arr); obtain the array value
  • Array_keys ($ arr); get the key name of the array
  • Array_flip ($ arr); values in the array are exchanged with key names (if there is a duplicate, the previous one will be overwritten)
  • In_array ("apple", $ arr); search for apple in the array
  • Array_search ("apple", $ arr); search for apple in the array. if a return key name exists
  • Array_key_exists ("apple", $ arr); retrieves whether the specified key name exists in the array.
  • Isset ($ arr [apple]): retrieves whether a given key name exists in the array.

Internal pointer of array

  • Current ($ arr); returns the current unit in the array
  • Pos ($ arr); returns the current unit in the array
  • Key ($ arr); returns the key name of the current unit in the array
  • Prev ($ arr); returns the internal pointer in the array to one
  • Next ($ arr); move the internal pointer in the array one by one
  • End ($ arr); points 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); returns the structure of a key name/value of the current element of the array, and moves the array pointer one byte forward.
  • List ($ key, $ value) = each ($ arr); obtain the key name and value of the current element of the array.

Conversion between arrays and variables

Extract ($ arr) is used to convert elements in the array into variables and import them to the current file. The key name is used as the variable name and the value is used as the variable value. Note: (the second parameter is very important. you can refer to the manual for usage) echo $ a; compact (var1, var2, var3); create an array with the given variable name

II. segmentation and filling of arrays

Array segmentation

  • Array_slice ($ arr,); removes a segment from the array. this function ignores the key name.
  • Array_splice ($ arr, array ("black", "maroon"); removes a segment of the array. Unlike the previous function, the returned sequence is deleted from the original array.

Split multiple arrays

  • Array_chunk ($ arr, 3, TRUE); you can divide an array into multiple values. TRUE indicates that the key name of the original array is retained.

Array filling

  • Array_pad ($ arr, 5, 'x'); fill an array to the specified length.
3. array and stack
  • Array_push ($ arr, "apple", "pear"); pushes one or more elements to the end of the array stack (inbound stack) and returns the number of inbound stack elements
  • Array_pop ($ arr); pops up the last element of the array stack (Out Stack)
IV. arrays and queues
  • Array_shift ($ arr); the first element in the array is removed and returned as a result (the length of the array is reduced by 1, the other elements are moved forward by one, and the number key name is changed to the zero technology, text key name unchanged)
  • Array_unshift ($ arr, "a", array (1, 2); insert one or more elements at the beginning of the array
V. callback functions
  • Array_walk ($ arr, 'function', 'Word'); use user functions to process each member in the array (the third parameter is passed to the callback function)
  • Array_mpa ("function", $ arr1, $ arr2); can process multiple arrays (their length should be the same when two or more arrays are used)
  • 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 is included in the returned result array, the key name of the array remains unchanged.
  • Array_reduce ($ arr, "function", "*"); convert to a single-value function (* as the first value of the array)
6. sorting arrays

Sort arrays by element values

  • Sort ($ arr); sort in ascending order (by which the second parameter is sorted) ignore the array sorting of key names
  • Rsort ($ arr); sort the key names in ascending order (the second parameter is sorted in what way)
  • Usort ($ arr, "function"); sort the values in the array using the user-defined comparison function (the function has two parameters, 0 indicates equal, A positive number indicates that the first value is greater than the second value, and a negative number indicates that the first value is smaller than the second value.) ignore the array sorting of key names.
  • Asort ($ arr); sort the key names in ascending order (in which the second parameter is sorted)
  • Arsort ($ arr); sort by ascending order (by which the second parameter is sorted)
  • Uasort ($ arr, "function"); sort the values in the array using the user-defined comparison function (the function has two parameters, 0 indicates equal, positive number indicates that the first key is greater than the second, negative number indicates that the first key is smaller than the second .)

Sort arrays by key name

  • Ksort ($ arr); sort by key name in the forward order
  • Krsort ($ arr); sort by key name in reverse order
  • Uksort ($ arr, "function"); sort the key names in the array using the user-defined comparison function (the function has two parameters, 0 indicates equal, A positive number indicates that the first is greater than the second, and a negative number indicates that the first is smaller than the second)

Sort by natural sorting

  • Natsort ($ arr); natural sorting (ignore key names)
  • Natcasesort ($ arr); natural sorting (case insensitive, key names ignored)
VII. array calculation

Sum of array elements

  • Array_sum ($ arr); calculates the sum of all elements in the array.

Array merging

  • Array_merge ($ arr1, $ arr2); merges two or more arrays (the same string key name, followed by the previous one, the same number key name, followed by no overwriting operation, instead, it is appended to the end.) "+" $ arr1 + $ arr2; only the last one is retained for the same key name.
  • Array_merge_recursive ($ arr1, $ arr2); recursive merge operation. If the array contains the same string key name, these values are merged into an array. If a value is an array, it is merged into another array according to the corresponding key name. When the array has the same array key name, the latter value will not overwrite the original value, but will be appended

Array difference set

  • Array_diff ($ arr1, $ arr2); returns the result array of the difference set.
  • Array_diff_assoc ($ arr1, $ arr2, $ arr3); returns the result array of the difference set and compares the key names.

Array intersection

  • Array_intersect ($ arr1, $ arr2); returns an array of intersection results
  • Array_intersect_assoc ($ arr1, $ arr2); returns an array of intersection results and compares the key names.
8. other array functions

Sum of array elements

  • Range (); create an array containing the specified range units
  • Array_unique ($ arr); removes repeated values from the array. the original key name is retained in the new array.
  • Array_reverse ($ arr, TRUE); returns an array in the opposite unit order to the original array. if the second parameter is TRUE, the original key name is retained.
  • Srand (float) microtime () * 10000000); random seed trigger
  • Array_rand ($ arr, 2); randomly retrieves one or more elements from the array.
  • Shuffle ($ arr); disrupt the order of arrays

Bytes. You can get twice the result with familiarity with related functions. Are you familiar with the following array functions? I. basic functions of array operations...

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.