Php array merging, splitting, and difference value function set _ PHP Tutorial

Source: Internet
Author: User
Function set for merging, splitting, and distinguishing php arrays. The merging array has three functions: 1. array_combine () carries two parameter arrays. The value of parameter array 1 serves as the key of the new array, and the value of parameter array 2 serves as the value of the new array. Very simple. Example: merging arrays has three functions:

1. array_combine ()

It carries two parameter arrays. The value of parameter array 1 serves as the key of the new array, and the value of parameter array 2 serves as the value of the new array. Very simple.

Example:

The code is as follows:


$ A = array ('green', 'red', 'yellow ');
$ B = array ('avcado', 'apple', 'bana ');
$ C = array_combine ($ a, $ B );

Print_r ($ c );
?>


The above example will output:

The code is as follows:


Array
(
[Green] => avocado
[Red] => apple
[Yellow] => banana
)



2. array_merge ()

It carries two parameter arrays and simply adds array 2 to the end of array 1 to form a new array.

Example:

The code is as follows:


$ Array1 = array ("color" => "red", 2, 4 );
$ Array2 = array ("a", "B", "color" => "green", "shape" => "trapezoid", 4 );
$ Result = array_merge ($ array1, $ array2 );
Print_r ($ result );
?>


The above example will output:

The code is as follows:


Array
(
[Color] => green
[0] => 2
[1] => 4
[2] =>
[3] => B
[Shape] => trapezoid
[4] => 4
)



3. array_merge_recursive ()

Similar to the above function, the only difference is that when the append finds that the key to be added already exists, the array_merge () processing method overwrites the previous key value, and array_merge_recursive () the processing method is to reconstruct the sub-array and combine the repeated key values into a new value array.

Example:

The code is as follows:


$ Ar1 = array ("color" => array ("favorite" => "red"), 5 );
$ Ar2 = array (10, "color" => array ("favorite" => "green", "blue "));
$ Result = array_merge_recursive ($ ar1, $ ar2 );
?>


In the preceding example, $ result is output:

The code is as follows:


Array
(
[Color] => Array
(
[Favorite] => Array
(
[0] => red
[1] => green
)

[0] => blue
)

[0] => 5
[1] => 10
)


The split array has two functions:

1. array_slice ()

Three parameters are included. one parameter is the target array, the other parameter is offset, and the other parameter is length. Function: extracts the child array with the length starting from offset from the target array.

If the offset value is positive, the start position is used to check the offset value from the beginning of the array. if the offset value is negative, the start position is used to check the offset value from the end of the array. If length is a positive number, the number of child array elements retrieved is undoubtedly length. if length is a negative number, the child array starts from offset to count (the target array) from the beginning of the array) -| length | end. In particular, if length is null, the end position is at the end of the array.

Example:

The code is as follows:


$ Input = array ("a", "B", "c", "d", "e ");

$ Output = array_slice ($ input, 2); // returns "c", "d", and "e"
$ Output = array_slice ($ input,-2, 1); // returns "d"
$ Output = array_slice ($ input, 0, 3); // returns "a", "B", and "c"

// Note the differences in the array keys
Print_r (array_slice ($ input, 2,-1 ));
Print_r (array_slice ($ input, 2,-1, true ));
?>


The above example will output:

The code is as follows:


Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)


2. array_splice ()

It carries three parameters, the same as above, to delete the child array with length starting from offset.

Example:

The code is as follows:


$ Input = array ("red", "green", "blue", "yellow ");
Array_splice ($ input, 2 );
// $ Input is now array ("red", "green ")

$ Input = array ("red", "green", "blue", "yellow ");
Array_splice ($ input, 1,-1 );
// $ Input is now array ("red", "yellow ")

$ Input = array ("red", "green", "blue", "yellow ");
Array_splice ($ input, 1, count ($ input), "orange ");
// $ Input is now array ("red", "orange ")

$ Input = array ("red", "green", "blue", "yellow ");
Array_splice ($ input,-1, 1, array ("black", "maroon "));
// $ Input is now array ("red", "green ",
// "Blue", "black", "maroon ")

$ Input = array ("red", "green", "blue", "yellow ");
Array_splice ($ input, 3, 0, "purple ");
// $ Input is now array ("red", "green ",
// "Blue", "purple", "yellow ");
?>



There are four different value functions:

1. array_intersect ()

Returns an array consisting of values of common elements in all arrays. the keys of an array are given by the keys of the first array.

Example:

The code is as follows:


$ Array1 = array ("a" => "green", "red", "blue ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_intersect ($ array1, $ array2 );
?>


The above example will output:

The code is as follows:


Array
(
[A] => green
[0] => red
)



2. array_intersect_assoc ()

Based on the previous function, return all key-value pairs with the same keys and values in the array.

Example:

The code is as follows:


$ Array1 = array ("a" => "green", "B" => "brown", "c" => "blue", "red ");
$ Array2 = array ("a" => "green", "yellow", "red ");
$ Result_array = array_intersect_assoc ($ array1, $ array2 );
?>


The above example will output:

The code is as follows:


Array
(
[A] => green
)


3. array_diff ()

Carry multiple arrays and return a new array consisting of all values in the first array but not in the following array. the corresponding key is taken from the first array.

Example:

The code is as follows:


$ Array1 = array ("a" => "green", "red", "blue", "red ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_diff ($ array1, $ array2 );

Print_r ($ result );
?>


The above example will output:

The code is as follows:


Array
(
[1] => blue
)



4. array_diff_assoc ()

Based on the previous function, not only the matching value but also the matching key are required.

Example:

The code is as follows:


$ Array1 = array ("a" => "green", "B" => "brown", "c" => "blue", "red ");
$ Array2 = array ("a" => "green", "yellow", "red ");
$ Result = array_diff_assoc ($ array1, $ array2 );
?>


The above example will output:

The code is as follows:


Array
(
[B] => brown
[C] => blue
[0] => red
)

Array 1. array_combine () carries two parameter arrays. The value of parameter array 1 is the key of the new array, and the value of parameter array 2 is the value of the new array. Very simple. Example :...

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.