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:
Copy CodeThe code is as follows:
$a = array (' green ', ' red ', ' yellow ');
$b = Array (' avocado ', ' apple ', ' banana ');
$c = Array_combine ($a, $b);
Print_r ($c);
?>
The example above will output:
Copy CodeThe code is 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 to form a new array.
Example:
Copy CodeThe code is as follows:
$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 CodeThe code is as follows:
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:
Copy CodeThe code is as follows:
$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:
Copy CodeThe code is as follows:
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:
Copy CodeThe 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 example above will output:
Copy CodeThe code is as follows:
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:
Copy CodeThe 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 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:
Copy CodeThe code is as follows:
$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 CodeThe code is as follows:
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:
Copy CodeThe code is as follows:
$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 CodeThe code is as follows:
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:
Copy CodeThe code is as follows:
$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 CodeThe code is as follows:
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:
Copy CodeThe code is as follows:
$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 CodeThe code is as follows:
Array
(
[B] = Brown
[C] = Blue
[0] = Red
)
http://www.bkjia.com/PHPjc/321300.html www.bkjia.com true http://www.bkjia.com/PHPjc/321300.html techarticle merging arrays 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: ...