_php techniques for merging, splitting and distinguishing function sets of PHP arrays

Source: Internet
Author: User
The merged array has three functions:

1.array_combine ()

carries two parameter arrays, the value of the parameter array one as the key of the new array, and the value of the parameter array two as the value of the new array. Very simple.

Example:
Copy Code code as follows:

<?php
$a = array (' green ', ' red ', ' yellow ');
$b = Array (' avocado ', ' apple ', ' banana ');
$c = Array_combine ($a, $b);

Print_r ($c);
?>

The example above will output:
Copy Code code as follows:

Array
(
[Green] => avocado
[Red] => Apple
[Yellow] => Banana
)


2.array_merge ()

carries two parameter arrays, simply appending the array two to the back of the array one to form a new array.

Example:
Copy Code code as follows:

<?php
$array 1 = Array ("Color" => "Red", 2, 4);
$array 2 = Array ("A", "B", "Color" => "green", "shape" => "trapezoid", 4);
$result = Array_merge ($array 1, $array 2);
Print_r ($result);
?>

The example above will output:
Copy Code code as follows:

Array
(
[Color] => Green
[0] => 2
[1] => 4
[2] => a
[3] => b
[Shape] => trapezoid
[4] => 4
)


3.array_merge_recursive ()

The only difference is that when the key you want to add already exists when you append it, the Array_merge () is handled by overwriting the preceding key value, and array_merge_recursive () by refactoring the child array to form a new numeric array of duplicate key values.

Example:
Copy Code code as follows:

<?php
$ar 1 = Array ("COLOR" => Array ("Favorite" => "Red"), 5);
$ar 2 = Array ("COLOR" => Array ("Favorite" => "green", "Blue"));
$result = array_merge_recursive ($ar 1, $ar 2);
?>

The previous example will output $result:
Copy Code code as follows:

Array
(
[Color] => Array
(
[Favorite] => Array
(
[0] => Red
[1] => Green
)

[0] => Blue
)

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

The split group has two functions:

1.array_slice ()

Carry three parameters, parameter one is target array, parameter two is offset, parameter three is length. The function is to remove a child array from the target array that is length from offset.

If offset is a positive number, the starting position checks for offset at the beginning of the array, if offset is a negative starting position, from the end of the array. If length is positive, then there is no doubt that the number of child array elements taken is length, and if length is negative, then the child array ends at offset from the beginning of the array at the beginning of count (destination array)-|length|. Specifically, if length is empty, the end position ends at the end of the array.

Example:
Copy Code code as follows:

<?php
$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"

The differences in the array keys
Print_r (Array_slice ($input, 2,-1));
Print_r (Array_slice ($input, 2,-1, true));
?>

The example above will output:
Copy Code code as follows:

Array
(
[0] => C
[1] => D
)
Array
(
[2] => C
[3] => D
)

2.array_splice ()

With three parameters, ditto, the effect is to delete the substring starting at length from offset.

Example:
Copy Code code as follows:

<?php
$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-taking functions:

1.array_intersect ()

With variable parameters, both arrays, returns an array of the values of the common elements in all arrays, and the key of the array is given by the key of the first array.

Example:
Copy Code code as follows:

<?php
$array 1 = Array ("A" => "green", "Red", "blue");
$array 2 = Array ("B" => "green", "yellow", "red");
$result = Array_intersect ($array 1, $array 2);
?>

The example above will output:
Copy Code code as follows:

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


2.array_intersect_assoc ()

On the basis of the previous function, returns the key value pairs that have the same key and values in all the arrays.

Example:
Copy Code code as follows:

<?php
$array 1 = Array ("A" => "green", "B" => "Brown", "C" => "Blue", "Red");
$array 2 = Array ("A" => "green", "yellow", "red");
$result _array = Array_intersect_assoc ($array 1, $array 2);
?>

The example above will output:
Copy Code code as follows:

Array
(
[A] => green
)

3.array_diff ()

Carries multiple arrays, returning a new array of all values that are not in the array in the first array, and the corresponding keys are taken from the first array.

Example:
Copy Code code as follows:

<?php
$array 1 = Array ("A" => "green", "Red", "Blue", "Red");
$array 2 = Array ("B" => "green", "yellow", "red");
$result = Array_diff ($array 1, $array 2);

Print_r ($result);
?>

The example above will output:
Copy Code code as follows:

Array
(
[1] => Blue
)


4.array_diff_assoc ()

On the basis of the previous function, you need to match not only the matching value but also the key.

Example:
Copy Code code as follows:

<?php
$array 1 = Array ("A" => "green", "B" => "Brown", "C" => "Blue", "Red");
$array 2 = Array ("A" => "green", "yellow", "red");
$result = Array_diff_assoc ($array 1, $array 2);
?>

The example above will output:
Copy Code code as follows:

Array
(
[B] => Brown
[C] => Blue
[0] => Red
)

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.