Introduction to common php array functions-Part 1

Source: Internet
Author: User
Introduction to common php array functions-Part 1

For beginners of php, it may be difficult to know what to learn about a large number of functions. I have listed some of the things that may be used in practice for your reference.

1) array_map (callable $ callback, array $ arr1 [, array $...])

Callback (callback function) receives a function: transmits the parameter order of arr1 to the preceding callback function.

  • When the callback function has parameters:
/* Example 1 */
 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125) **/?>
  • When the callback function has no parameters:
 Array ([0] => 1 [1] => one [2] => uno) [1] => Array ([0] => 2 [1] => two [2] => dos) [2] => Array ([0] => 3 [1] => three [2] => tres) [3] => Array ([0] => 4 [1] => four [2] => cuatro) [4] => Array ([0] => 5 [1] => five [2] => cinco) */?>

2) range (mixed $ start, mixed $ limit [, number $ step = 1])

Step indicates the interval value. If this parameter is left blank, the default value is 1.

/* Example 1 generates a set of numbers */$ nums = range (1, 5); print_r ($ nums);/* output: array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5) */$ nums = range (1, 5, 2); print_r ($ nums);/* output: array ([0] => 1 [1] => 3 [2] => 5 )*/
/* Example 2: generate an array of letters */$ array = range ('A', 'F'); print_r ($ array);/* output: array ([0] => a [1] => B [2] => c [3] => d [4] => e [5] => f) */$ array = array ('A', 'F', 2); print_r ($ array);/* output: array ([0] => a [1] => c [2] => e )*/

3) array_merge (array $ array1 [, array $...])

Array_merge () combines the units of one or more arrays. the values in an array are appended to the values of the previous array. Returns an array of results.

If the input array contains the same string key name, the value after the key name overwrites the previous value. However, if the array contains a number key name, the subsequent values will not overwrite the original values, but will be appended to the back.

If only one array is assigned and the array is indexed by number, the key name is re-indexed continuously.

/* Explanation: if only one array is assigned and the array is digitally indexed, the key name will be re-indexed continuously. */$ Array1 = array (1, 2, 3, 4, 5); $ array2 = array (1, 2, 8, 9); $ array3 = array_merge ($ array1, $ array2); print_r ($ array3);/* output: array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 1 [6] => 2 [7] => 8 [8] => 9) */

4) array_merge_recursive (array $ array1 [, array $...])Recursively merge one or more arrays

If the input array has the same string key name, these values will be merged into an array, which will be recursive. Therefore, if a value is an array, this function combines the corresponding entries into another array. However, if the array has the same array key name, the latter value will not overwrite the original value, but will be appended to the back.

Values are merged based on the same layer of key names.

/* Example 1 */$ ar1 = array ("color" => array ("favorite" => "red"), 5); $ ar2 = array (10, "color" => array ("favorite" => array ('a' => "red"), "blue"); $ result = array_merge_recursive ($ ar1, $ ar2); print_r ($ result);/* output: array ([color] => Array ([favorite] => Array ([0] => red [a] => red key) [0] => blue) [0] => 5 [1] => 10 )*/
/* Example 2 */$ ar1 = array ("color" => array ("favorite" => "red"), 5); $ ar2 = array (10, "color" => array ("favorite" => "red", "blue"); $ result = array_merge_recursive ($ ar1, $ ar2); print_r ($ result ); /* output: Array ([color] => Array ([favorite] => Array ([0] => red [1] => red key) [0] => blue) [0] => 5 [1] => 10 )*/

5) array_pad (array $ input, int $ pad_size, mixed $ pad_value)Add value to the array to the specified length. the original array will not change.

 

6) array_pop (array & $ array)When the last element is removed, the original array changes.

Array_pop () pops up, returns the last unit of the array, and deletes the length of the array by one.If array is NULL (or not an array), NULL is returned.. In addition, if the number is not called, a Warning is generated.

 /* Output Array ([0] => orange [1] => banana [2] => apple )*/

7) array_shift ()Removes the elements starting with an array from the array. the original array changes. when this function is used, the array pointer (reset () is reset.

Array_shift () removes the first unit of array and returns the result. This removes the length of array and moves all other units one bit forward. The names of all numeric keys are counted from scratch, and the names of text keys remain unchanged.If array is NULL (or not an array), NULL is returned.

8) int array_push (array & $ array, mixed $ var [, mixed $...])

  • Array_push () treats array as a stack and pushes the passed variables to the end of array. The length of array will increase according to the number of variables in the stack.
  • Returns the number of elements in the processed array.

9) reset (array & $ array)Point the internal pointer of the array to the first unit.

  • Reset () returns the internal pointer of array to the first unit and the value of the first array unit.
  • If the array is null, false is returned.

10) end (array & $ array)

Parameter array.Reference (&)Passed because it will be modified by this function. This means that you must inputReal variables, AndArray not returned by the functionBecause only real variables can be passed as references.

  • End () moves the internal pointer of array to the last unit and returns its value.
  • If the array is null, false is returned.
    
 
 

For reference transfer, if the return value of a function is not assigned to a variable, the memory will not be truly opened up with a separate space memory. The parameter passed by reference must have a real memory. Therefore, $ array = back () will open up a piece of memory for $ array variable.

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.