PHP array_merge Merge split two arrays _php tutorial

Source: Internet
Author: User
The Array_merge function of the array and the + operator are confusing, and the difference between them is found by writing a small program. In particular, the + operator, which means that the right array unit (DE-duplication) is appended to the left-hand array.
The code is as follows Copy Code

$arr 1=array ("A", "B", "C");
$arr 2=array ("C", "D", "E");

$myarray =array_merge ($arr 1, $arr 2);
Print_r ($myarray);

$myarray =array_unique ($myarray);

Print_r ($myarray);
?>

Cases

The code is as follows Copy Code

$array 1=array (1, 2);//array 1
$array 2=array (2, 3);//Array 2
$array 3=array_merge ($array 1, $array 2);//merge arrays;
$array 3=array_unique ($array 3);//Remove duplicate values from the divisor group
?>

Example

The code is as follows Copy Code

echo "RN The first case of 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 + $a;
Print_r ($c);


echo "RN The second case of 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 + $a;
Print_r ($c);


echo "RN The third case of 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 + $a;
Print_r ($c);
?>

The results are 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
)

The second case
Array
(
[0] = a
[1] = b
[2] = C
[3] = d
[4] = = E
[5] = f
[6] = a
[7] = X
[8] = y
)
Array
(
[0] = a
[1] = b
[2] = C
[3] = d
[4] = = E
[5] = f
)
Array
(
[0] = a
[1] = X
[2] = y
[3] = d
[4] = = E
[5] = f
)

The third case
Array
(
[0] = a
[1] = b
[2] = C
[3] = d
[4] = = E
[5] = f
[6] = a
[7] = X
[8] = y
)
Array
(
[1] = a
[2] = b
[3] = C
[4] = d
[5] = = E
[6] = f
[7] = X
[8] = y
)
Array
(
[1] = a
[7] = X
[8] = y
[2] = b
[3] = C
[4] = d
[5] = = E
[6] = f
)

Split fraction Group Array_slice ()
The Array_slice () function returns a portion of the array, starting with the key offset and ending at the offset+length position. Its form:

PHP code
1.array array_slice (array array, int offset[,int length])
Array array_slice (array array, int offset[,int length])
When offset is positive, the split starts at offset from the beginning of the array, and if offset is a negative value, the split starts at the offset position at the end of the array. If the optional parameter length is omitted, the split starts at offset and continues to the last element of the array. If length is given and is positive, the offset+length position ends at the beginning of the array. Conversely, if length is given and is negative, the count (Input_array)-|length| position at the beginning of the array ends. Consider an example:

PHP code

The code is as follows Copy Code

$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 Copy Code

$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)
?>

http://www.bkjia.com/PHPjc/629036.html www.bkjia.com true http://www.bkjia.com/PHPjc/629036.html techarticle the Array_merge function of the array and the + operator are confusing, and the difference between them is found by writing a small program. In particular, the + operator, he means, will be the right array unit (DE-emphasis ...

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