PHParray_merge combines and splits two arrays. The array_merge function and the + operator of the array are confused. I wrote a small program to compare them and found their differences. In particular, the + operator means to confuse the array units on the right (the array_merge function and the + operator of the array are de-duplicated, I wrote a small program and compared them to find their differences. In particular, the + operator, which means that the array unit on the right (deduplication) is appended to the back of the array on the left.
| The code is as follows: |
|
|
$ Arr1 = array ("a", "B", "c ");
$ Arr2 = array ("c", "d", "e ");
$ Myarray = array_merge ($ arr1, $ arr2 );
Print_r ($ myarray );
$ Myarray = array_unique ($ myarray );
Print_r ($ myarray );
?> |
Example
| The code is as follows: |
|
|
$ Array1 = array (1, 2); // array 1
$ Array2 = array (2, 3); // array 2
$ Array3 = array_merge ($ array1, $ array2); // merge arrays;
$ Array3 = array_unique ($ array3); // Remove repeated values from the array
?> |
Example
| The code is as follows: |
|
|
Echo "rn First case rn ";
$ A = array (1, 2, 3, 4, 5, 6 );
$ B = array (7,8, 9 );
$ C = array_merge ($ a, $ B );
Print_r ($ c );
$ C = $ a + $ B;
Print_r ($ c );
$ C = $ B + $;
Print_r ($ c );
Echo "rn second case rn ";
$ A = array ('A', 'B', 'C', 'D', 'e', 'F ');
$ B = array ('A', 'X', 'y ');
$ C = array_merge ($ a, $ B );
Print_r ($ c );
$ C = $ a + $ B;
Print_r ($ c );
$ C = $ B + $;
Print_r ($ c );
Echo "rn case 3 rn ";
$ A = array (
1 => 'A ',
2 => 'B ',
3 => 'C ',
4 => 'D ',
5 => 'e ',
6 => 'F ');
$ B = array (
1 => 'A ',
7 => 'X ',
8 => 'y ');
$ C = array_merge ($ a, $ B );
Print_r ($ c );
$ C = $ a + $ B;
Print_r ($ c );
$ C = $ B + $;
Print_r ($ c );
?>
The result is as follows:
First case
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
Array
(
[0] => 7
[1] => 8
[2] => 9
[3] => 4
[4] => 5
[5] => 6
)
Case 2
Array
(
[0] =>
[1] => B
[2] => c
[3] => d
[4] => e
[5] => f
[6] =>
[7] => x
[8] => y
)
Array
(
[0] =>
[1] => B
[2] => c
[3] => d
[4] => e
[5] => f
)
Array
(
[0] =>
[1] => x
[2] => y
[3] => d
[4] => e
[5] => f
)
Case 3
Array
(
[0] =>
[1] => B
[2] => c
[3] => d
[4] => e
[5] => f
[6] =>
[7] => x
[8] => y
)
Array
(
[1] =>
[2] => B
[3] => c
[4] => d
[5] => e
[6] => f
[7] => x
[8] => y
)
Array
(
[1] =>
[7] => x
[8] => y
[2] => B
[3] => c
[4] => d
[5] => e
[6] => f
) |
Split array array_slice ()
The array_slice () function returns a part of the array, starting from the key offset and ending at the offset + length position. Form:
Php code
1. array array_slice (array, int offset [, int length])
Array array_slice (array, int offset [, int length])
When offset is a positive value, the split starts from the offset position starting from the array. if offset is a negative value, the split starts from the offset position at the end of the array. If the optional length parameter is omitted, the split starts from offset to the last element of the array. If length is given and it is a positive number, it will end at the offset + length position starting with the array. On the contrary, if length is given and it is a negative number, it ends at the count (input_array)-| length | position starting with the array. Consider an example:
Php code
| The code is as follows: |
|
|
$ Fruits = array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon ");
$ Subset = array_slice ($ fruits, 3 );
Print_r ($ subset );
// Output
// Array ([0] => Pear [1] => Grape [2] => Lemon [3] => Watermelon)
?> |
Then we use the following negative length:
Php code
| The code is as follows: |
|
|
$ Fruits = array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon ");
$ Subset = array_slice ($ fruits, 2,-2 );
Print_r ($ subset );
// Output
// Array ([0] => Orange [1] => Pear [2] => Grape)
?> |
Bytes. In particular, the + operator means to remove the array units (de-duplicated...