Array functions: splitting, merging, splitting, and joining

Source: Internet
Author: User

The array processing function described in this section can complete more complex array processing tasks and process arrays as a set. For example, you can merge multiple arrays of two goods, calculate the difference set or intersection between arrays, extract part of the array elements, and compare the arrays.

① Function array_slice ()

The array_slice () function is used to retrieve a value in the Array Based on the Conditions and return it. If the array has a string key, the returned array retains the key name. You can set four parameters for this function. The prototype of the function is as follows:

Array array_slice (array, int offset [, int length [, bool preserver_keys])

The first parameter 'array' is required. input the array to be processed during the call. The second parameter offset is also a required parameter. A value must be input to specify the start position of the element to be retrieved. If it is a positive number, it is taken from the beginning to the end. If it is a negative value, it is used to take the absolute logarithm of the offset from the back to the end. The third parameter is optional. You also need to input a value to specify the length of the returned array. If it is a negative number, select the absolute logarithm element of the value from the forward. If this value is not set, all elements are returned. The fourth parameter is optional. A Blair value is required. If the value is not true, the returned array retains the key name. If this parameter is set to false, the index key is reset by default. Note that if the array has a string key, the returned array retains the key name. The code used by the array_slice () function is as follows:

12345678910111213141516 <?php$lamp=array("Linux", "Apache", "MySQL", "PHP");    // Use array_slice () to get from the second (0 is the first, 1 is the second), take two elements and return them from the array $ lamp.    print_r(array_slice($lamp, 1,2));       // Output array ([0] => Apache [1] => MySQL)     // The second parameter uses a negative value of-2, which is obtained from the first and second faces and returns an element.    print_r(array_slice($lamp, -2,1));        // Output array ([0] => MySQL)     // The last parameter is set to true, and the original key value is retained.    print_r(array_slice($lamp, 1,2, true));      // Output array ([1] = Apache [2] => MySQL) $lamp=array("a"=>"Linux", "b"=>"Apache", "c"=>"MySQL", "d"=>"PHP");     // If the array contains a string key, the returned array retains the key name.    print_r(array_slice($lamp, 1,2));       // Output array ([B] => Apache [c] => MySQL)?>

② Function array_splice ()

The array_splice () function is similar to the array_slice () function. It selects a series of elements in the array but does not return them. Instead, it deletes them and replaces them with other values. If the fourth parameter is provided, the selected elements Replace the array specified by the fourth parameter. The final generated array will be returned. The function prototype is as follows:

Array array_splice (array & input, int offset [, int length [, array replacement])

The first parameter 'array' is required and specifies the array to be processed. The second parameter offset is also required. A value is input during the call. If the offset value is positive, it is removed from the offset specified by this value in the input array. If offset is negative, it is removed from the offset specified by the reciprocal value at the end of the input array. The third parameter length is optional and also requires a value. If this parameter is omitted, all parts from the offset to the end of the array are removed. If length is specified and it is a positive value, so many elements are removed. If length is specified and it is a negative value, all the elements from the offset to the reciprocal length at the end of the array are removed. The fourth parameter array is optional. The removed element is replaced by the element in the array. If no value is removed, the elements in this array are inserted to the specified position. The code used by the array_splice () function is as follows:

123456789101112131415161718192021 <?php    $input = array("Linux", "Apache", "MySQL", "PHP");    // The second element in the original array is deleted at the end of the array.    array_splice($input,2);    print_r($input);         // Output array ([0] => Linux [1] => Apache)     $input = array("Linux", "Apache", "MySQL", "PHP");    // Remove all the elements from the second element until the last one at the end of the array.    array_splice($input,1,-1);    print_r($input)          // Output array ([0] => Linux [1] => PHP)     $input = array("Linux", "Apache", "MySQL", "PHP");    // All elements from the second element to the end of the array are replaced by the fourth parameter.    array_splice($input,1,count($input),"web");    print_r($input)          // Output array ([0] => Linux [1] => Web)     $input = array("Linux", "Apache", "MySQL", "PHP");    // The last element is replaced by the fourth parameter array.    array_splice($input,-1,1,array("web","www"));    print_r($input)          // Output array ([0] => Linux [1] => Web [2] => MySQL [3] => Web [4] => www)?>

③ Function array_combine ()

The array_comblie () function is used to create a new array by combining two arrays. One array is the key name, and the other array value is the key value. If one of the arrays is empty or the number of elements in the two arrays is different, the function returns false. The function prototype is as follows:

Array array_combine (array keys, array values)

This function has two parameters and is required. The two parameters must have the same number of elements. The code used by the array_combine () function is as follows:

1234567 <?php    $a1 = array("OS","WebServer","DataBase","Language");     // Declare the first array as parameter 1    $a2 = array("Linux","Apache","MySQL","PHP")              // Declare the second array as parameter 2     print_r(array_combine($a1,$a2));                         // Use array_combine () to merge the two Arrays    // Output array ([OS] => Linux [webserver] => Apache [database] => MySQL [language] => PHP)?>

④ Function array_merge ()

The array_merge () function combines one or more numbers into an array. If the key name already exists, the value of the key corresponds to the value of the last key name (the latter overwrites the previous one ). If the array is a digital index, the key name is re-indexed continuously. Note that if you only input an array to the array_merge () function and the key name is an integer, the function returns a new array with an integer key name, its key name starts from 0 and is re-indexed. The function prototype is as follows:

Array array_merge (array array1 [, array array2 [, array...])

The first parameter of the function is required and an array needs to be input. There can be multiple optional parameters, but they must all be array-type data. Returns the new array that combines multiple numbers. The code used by the array_merge () function is as follows:

12345678910 <?php    $a1 = array("a"=>"Linux","b"=>"Apache");    $a2 = array("c"=>"MySQL","b"=>"PHP");     print_r(array_merge($a1,$a2));        // Output array ([a] => Linux [B] => PHP [c] => MySQL)     // If only one array parameter is used, the key name starts from 0 and is re-indexed.    $a = array(3=>"php",4=>"MySQL");    print_r(array_merge($a));             // Output array ([0] => PHP [1] => MySQL)?>

⑤ Function array_intersect ()

The array_intersect () function is used to calculate the intersection of arrays. The returned result array contains all values in the compared array and all other parameter arrays. The key name remains unchanged and only values are used for comparison. The function prototype is as follows:

Array array_intersect (array array1, array array2 [, array...])

The first parameter of this function is required. It is the first array compared with other arrays. The second parameter is also a required parameter. It is an array compared with the first array. Multiple optional parameters can be used as subsequent parameters to compare them with the first array. The code used by the array_intersect () function is as follows:

123456 <?php    $a1 = array("Linux", "Apache", "MySQL", "PHP");    $a2 = array("Linux", "Tomcat", "MySQL", "JSP");     print_r(array_intersect($a1,$a2));        // Output array ([0] => Linux [2] => MySQL)?>

⑥ Function array_diff ()

The array_diff () function is used to return the number group of difference sets of two arrays (elements with different values ). This array includes all element values in the compared array, but not in any other parameter array. In the returned array, the key name remains unchanged. The function prototype is as follows:

Array array_diff (array array1, array array2 [, array...])

The first parameter is required, and an array to be compared with other arrays is passed in. The second parameter is also required, and an array to be compared with the first array is passed in. The third parameter is optional later, and one or more Arrays can be compared with the first array. This function only has values for comparison. The code used by the array_diff () function is as follows:

123456 <?php    $a1 = array("Linux", "Apache", "MySQL", "PHP");    $a2 = array("Linux", "Tomcat", "MySQL", "JSP");     print_r(array_diff($a1,$a2));        // Output array ([1] => Apache [3] => PHP)?>

In the preceding example, when two arrays are compared using the array_diff () function, the elements that exist in the first array and are not in the second array are returned.

 

> Fixed link: http://php.ncong.com/php_course/arry_function/chaifei_hebing_fenjie_jiehe.html

> For Reprinted Information, please note: ncong PHP was published in ncong PHP learning tutorial on September 10, April 14, 2014.

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.