Common PHP array function usage summary, php array summary _ PHP Tutorial

Source: Internet
Author: User
Summary of common PHP array functions and php array. Summary of common PHP array functions, and php array summary examples in this article describe common PHP array functions. For your reference, the details are as follows: 1. arrayarray_merge (array $ a PHP common array function usage summary, php array summary

This example describes common PHP array functions. 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.

For details about the usage of array_slice, refer to the previous article "array_slice function usage example in PHP".

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.

<?phpfunction cube($n){  return($n * $n * $n);}$a = array(1, 2, 3, 4, 5);$b = array_map("cube", $a);print_r($b);?>

Output:

Array(  [0] => 1  [1] => 8  [2] => 27  [3] => 64  [4] => 125)

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.