Home  >  Article  >  Backend Development  >  PHP array_merge merge and split two arrays_PHP tutorial

PHP array_merge merge and split two arrays_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:14:091117browse

I was confused about the array_merge function and + operator of arrays, so I wrote a small program to compare them and found their differences. Especially the + operator, which means to append the right array unit (to remove duplicates) to the left array.

The code is as follows Copy code
 代码如下 复制代码

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

$arr1=array("a","b","c");
$arr2=array("c","d","e");

$myarray=array_merge($arr1,$arr2);
print_r($myarray);

$myarray=array_unique($myarray);

代码如下 复制代码

$array1=array(1, 2);//数组1
$array2=array(2, 3);//数组2
$array3=array_merge($array1, $array2);//合并数组;
$array3=array_unique($array3);//移除数组中重复的值
?>

print_r($myarray);
?> Example
The code is as follows Copy code
$array1=array(1, 2);//array 1
$array2=array(2, 3);//Array 2
$array3=array_merge($array1, $array2);//Merge arrays;
$array3=array_unique($array3);//Remove duplicate values ​​in the array
?>

 

例子

 代码如下 复制代码
 代码如下 复制代码

echo "rn第一种情况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第二种情况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第三种情况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);
?>

结果如下:

第一种情况
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
)
 
第二种情况
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
)
 
第三种情况
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
)

echo "rn第一种情况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第二种情况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第三种情况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);
?> 结果如下: 第一种情况
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
)
 
第二种情况
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
)
 
第三种情况
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 array array_slice()
The array_slice() function will return a portion of the array, starting from the key offset and ending at offset+length. 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 a positive value, splitting will start at offset from the beginning of the array; if offset is negative, splitting will start at offset from the end of the array. If the optional length parameter is omitted, the split will start at offset and go to the last element of the array. If length is given and is positive, it ends at offset+length from the beginning of the array. Conversely, if length is given and is negative, it ends at count(input_array)-|length| from the beginning of 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 )
?>

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

$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 lower 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.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629036.htmlTechArticleI was confused about the array_merge function and the + operator of arrays, so I wrote a small program to compare and found their differences. . Especially the + operator, what he means is to remove the duplication of the array unit on the right...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn