Summary of common php array function instances

Source: Internet
Author: User
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



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.



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!

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: 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.