This article mainly introduces common php array functions, and summarizes and analyzes the functions and usage skills of common php array functions array_merge, array_slice, and array_map in combination with examples, for more information about php array functions, see the examples in this article. We will share this with you for your reference. The details are as follows:
1. array array_merge (array $ array1 [, array $ array2 [, $ array])
Function: combines units of one or more arrays. values of an array are appended to the previous array. The array of returned 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.
Example 1: The array has the same string key name
$array1=array('color'=>'greed','3'=>8);$array2=array("a",'color'=>"red",'3'=>8);var_dump(array_merge($array1,$array2));
Output:
array(4){ ["color"]=> string(3) "red" [0]=> int(8) [1]=> string(1) "a" [2]=> int(8)}
The color with duplicate key names is overwritten. only the 'red' value is taken. the '3' with the same key name is not overwritten, but is appended to the back.
2. array array_slice (array $ array, int $ offset, [, int $ length [, bool $ preserve_keys])
Function: return a sequence in the array specified by the offset and length parameters.
If offset is not negative, the array starts from the offset from the start end to the offset. $ Array = array (,); array_slice ($ array, 2); equivalent to (,); red represents the truncated array
If offset is negative, the array starts from the offset from the end to the offset. Array_slice ($ array,-2); equivalent to (1, 2, 3, 4, 5, 6 );
If the length is positive, it indicates the number of interceptions starting from offset. Array_slice ($ array, 2, 2); equivalent to (1, 2, 3, 4, 5, 6 );
If length is negative, the sequence is terminated so far from the end of the array; array_slice ($ array, 2,-1); equivalent );
The default value is: to the end of the array.
3. array_map (callback $ callback, array $ arr1 [, array $ ......])
Function: returns an array that contains all the units in arr1 after the callback function. The number of parameters accepted by callback should be the same as the number of arrays passed to the array_map () function.
Output:
Array( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125)
I hope this article will help you with PHP programming.
For more articles about examples of common php array functions, refer to the PHP Chinese network!