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

$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 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

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

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

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

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

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

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles