Follow me to learn PHP array common functions-previous

Source: Internet
Author: User
Tags learn php

For beginners of PHP, it may be unclear to a large number of its functions what to learn. I have listed here some of the actual things you may be using, for your reference

1) Array_map (callable $callback, array $arr 1 [, Array $ ...])

Callback (callback function), accepts a function function: passes the ARR1 parameter order to the preceding callback function

    • When the callback function has parameters:
/* Example 1*/
 
   1     [1] = 8     [2] =     [3] =     [4] = +) **/?>
    • When the callback function has no parameters:
 
  Array        (            [0] = 1            [1] = one            [2] = Uno        )    [1] = = Array        (            [0] = 2            [1] = [            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 represents the interval value, no write default is 1

/* Example 1, generate a set of numbers */$nums = range (1, 5);p Rint_r ($nums);/* Output: Array ([0] = 1 [1] + 2 [2] = 3 [3] = 4 [4] = 5) */$nums = range (1, 5, 2);p Rint_r ($nums);/* Output: Array ([0] = 1 [1] = 3 [2] = 5) */
/* Example 2, produces a set of alphabetic arrays */$array = range (' A ', ' f ');p Rint_r ($array);/* Output: Array ([0] = a [1] = = b [2] = c [3] = d [4] =& Gt  e [5] = f) */$array = array (' A ', ' F ', 2);p Rint_r ($array);/* Output: Print_r ($array);/* Output: Array ([0] = a [1] = c [2] + e) */

3) Array_merge (array $array 1 [, Array $ ...])

Array_merge () Merges the cells of one or more arrays, and the values in an array are appended to the previous array. Returns an array as the result.

If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains numeric key names, subsequent values will not overwrite the original values, but are appended to the back.

If only one array is given and the array is a numeric index, the key name is re-indexed in a sequential manner.

/* Explanation: If only one array is given and the array is a numeric index, the key name is re-indexed in a sequential manner. */$array 1 = Array (1, 2, 3, 4, 5), $array 2 = Array (1, 2, 8, 9), $array 3 = Array_merge ($array 1, $array 2);p Rint_r ($array 3);/* 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 $array 1 [, Array $ ...]) to merge one or more arrays recursively

If the input array has the same string key name, the values are merged into an array, which recursively goes down, so if a value is itself an array, the function will merge it into another array according to the corresponding entry. However, if the array has the same array key name, the latter value will not overwrite the original value, but is appended to the back.

Merges values according to the same layer and layer of key names

/* example 1*/$ar 1 = Array ("color" = = Array ("Favorite" = "Red"), 5), $ar 2 = Array (10, " Color "= = Array (" favorite "= = Array (' a ' = =" Red ")," Blue ")); $result = Array_merge_recursive ($ar 1, $ar 2);p Rint_r                    ($result);/* Output: Array ([color] = = Array ([favorite] = = Array (     [0] = = Red [A] = red focus) [0] = blue) [0] = 5 [1] = +) */
/* Example 2*/$ar 1 = Array ("color" = = Array ("Favorite" = "Red"), 5), $ar 2 = Array ("color" = = "Array" ("Favorite" = "R" Ed "," Blue ")), $result = Array_merge_recursive ($ar 1, $ar 2);p Rint_r ($result);/* Output: Array (    [color] = = Array        (            [favorite] = = Array                (                    [0] = red                    [1] = red  focus                )            [0] = blue        )    [0] = 5    [1] = 10) */

5) Array_pad (array $input, int $pad _size, mixed $pad _value) adds value to the specified length to the array, the original array does not change

 
  

6) Array_pop (array & $array) move out the last element, the original array will change

Array_pop () pops up and returns the last cell of the array, minus one of the array's length. if array is empty (or not an array), NULL is returned . Also, if the call is not a number, a Warning will be generated.

 
  /* Output Array (    [0] = orange    [1] = = Banana    [2] = + apple) */

7) Array_shift () moves the cell at the beginning of the array to the array, which is changed, and the array pointer is reset (reset ()) after using this function.

Array_shift () Moves the first cell of the array out and returns as a result, reducing the length of the array and moving all other cells forward one bit. All numeric key names are changed from zero to start, and the text key name is unchanged. NULL is returned if array is empty (or not an array)

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

    • Array_push () treats the array as a stack and presses the incoming variable into the end of the array. The length of the array will be incremented according to the number of variables in the stack
    • Returns the number of elements in the array after processing

9) Reset (array & $array) points The inner pointer of the array to the first cell

    • Reset () returns the internal pointer of the array back to the first cell, returning the value of the first array cell.
    • Array is null returns FALSE

) End (array & $array)

The parameter array, which is passed by Reference (&) , because it is modified by this function. This means that you must pass in a real variable , not an array returned by the function , because only the real variable can be passed by reference.

    • End () Moves the internal pointer of the array to the last cell and returns its value.
    • Array is null returns FALSE
!--? phpfuncation back () {$arr = Array (1, 2, 3); return $arr;} */* The above refers to the meaning of the delivery please note * * $num = end (back ()); Error! Because the argument is a reference to pass $array = back (); $num = end ($array); Correct  
  
  

For reference passing, if you do not assign a function's return value to a variable, it will not actually open up in memory as a separate space memory. The arguments passed by reference must require a real memory, so $array = back (), which opens up a memory to the $array variable

  • Related Article

    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.