Home Backend Development PHP Tutorial PHP array_merge merge and split two arrays_PHP tutorial

PHP array_merge merge and split two arrays_PHP tutorial

Jul 13, 2016 pm 05:14 PM
array merge php function merge and right Split array Compare of operator

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
 代码如下 复制代码

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

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

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

$myarray=array_unique($myarray);

代码如下 复制代码

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

 

例子

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

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

<?php
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
 代码如下 复制代码

<?php

$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

<?php

代码如下 复制代码

<?php

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

$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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles