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

Source: Internet
Author: User
Merging, splitting, and distinguishing function sets of PHP arrays
It is said that the PHP array function is very powerful, only really used in the project work can feel, at least I think, now has slowly discovered the mystery of it ...
Merging arrays has three functions:
1.array_combine ()
carries two parameter array, 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:
$a = array (' green ', ' red ', ' yellow ');
$b = Array (' avocado ', ' apple ', ' banana ');
$c = Array_combine ($a, $b);

Print_r ($c);
?>

The example above will output:

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 to form a new array.
Example:
$arrayarray 1 = Array ("Color" = "Red", 2, 4);
$arrayarray 2 = Array ("A", "B", "color" = "green", "shape" = "trapezoid", 4);
$result = Array_merge ($array 1, $array 2);
Print_r ($result);
?>

The example above will output:
Array
(
[Color] = Green
[0] = 2
[1] = 4
[2] = a
[3] = b
[Shape] = trapezoid
[4] = 4
)
3.array_merge_recursive ()
Identical to the above function, the only difference is that when the appended key is found to be added, Array_merge () is handled by overwriting the previous key value, and array_merge_recursive () is treated as a refactoring sub-array, and the value of the duplicate key is formed into a new array of values.
Example:
$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 example above will output
$result:

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

[0] = Blue
)

[0] = 5
[1] = 10
)
There are two functions for splitting a fractional group:
1.array_slice ()
Carry three parameters, parameter one is the target array, parameter two is offset, parameter three is length. The function is to remove from the target array a subarray of length from offset start.
If offset is a positive number, the start position is checked at offset from the beginning of the array, if offset is a negative start position from the end of the array to check offset. If length is positive, there is no doubt that the number of sub-array elements fetched is length, and if length is negative, the sub-array ends at offset start and count (destination array)-|length| from the beginning of the array. Specifically, if length is empty, the end position is at the end of the array.
Example:
$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 example above will output:
Array
(
[0] = C
[1] = d
)
Array
(
[2] = C
[3] = d
)
2.array_splice ()
Carry three parameters, ibid., to delete a subarray of length from offset.
Example:
$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 difference accessor functions:
1.array_intersect ()
An array of the values of the common elements in all the arrays, which are given as the key of the first array, are the variables that carry the variable.
Example:
$arrayarray 1 = Array ("A" = "green", "Red", "blue");
$arrayarray 2 = Array ("b" = "green", "yellow", "red");
$result = Array_intersect ($array 1, $array 2);
?>

The example above will output:
Array
(
[A] = Green
[0] = Red
)
2.ARRAY_INTERSECT_ASSOC ()
On the basis of the previous function, returns a key-value pair with the same key and value in all arrays.
Example:
$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:
Array
(
[A] = Green
)
3.array_diff ()
Carries multiple arrays, returning a new array of all the values in the first array that are not in the following array, and the corresponding key is taken from the first array.
Example:
$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:
Array
(
[1] = Blue
)
4.ARRAY_DIFF_ASSOC ()
On the basis of the previous function, you need to match not only the value but also the key.
Example:
$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:
Array
(
[B] = Brown
[C] = Blue
[0] = Red
)

The author "The Life of Flying"

http://www.bkjia.com/PHPjc/478628.html www.bkjia.com true http://www.bkjia.com/PHPjc/478628.html techarticle the combination of PHP arrays, split, the difference value function set all say that the PHP array function is very powerful, only really used in project work can feel, at least I think is, now has slowly ...

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