Home Backend Development PHP Tutorial PHP array merging, splitting, and differential value function set_PHP tutorial

PHP array merging, splitting, and differential value function set_PHP tutorial

Jul 21, 2016 pm 03:40 PM
php function the difference value merge Split array have of

There are three functions for merging arrays:

1.array_combine()

carries two parameter arrays. The value of parameter array one is used as the key of the new array, and the value of parameter array two is value as the value of the new array. Very simple.

Example:

Copy code The code is as follows:

$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b) ;

print_r($c);
?>

The above example will output:
Copy code The code is as follows:

Array
(
[green] => avocado
[red] => apple
[yellow] => banana
)


2.array_merge()

carries two parameter arrays, and simply appends array two to the back of array one Create a new array.

Example:
Copy code The code is as follows:

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => " trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

The above example will output:
Copy code The code is as follows:

Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)


3.array_merge_recursive()

is the same as the above function, the only difference is that When appending, it is found that the key to be added already exists. The processing method of array_merge() is to overwrite the previous key value. The processing method of array_merge_recursive() is to reconstruct the sub-array and form a new numerical array with the values ​​of the repeated keys.

Example:
Copy code The code is as follows:

$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green ", "blue"));
$result = array_merge_recursive($ar1, $ar2);
?>

The above example will output $result:
Copy code The code is as follows:

Array
(
[color] => Array
(
[ favorite] => Array
(
[0] => red
[1] => green
)

[0] => blue
)

[0] => 5
[1] => 10
)

There are two functions for splitting an array:

1.array_slice()

carries three parameters. Parameter one is the target array, parameter two is offset, and parameter three is length. The function is to extract a subarray of length starting from offset from the target array.

If offset is a positive number, the starting position is checked from the beginning of the array. If offset is a negative number, the starting position is checked from the end of the array. If length is a positive number, the number of subarray elements taken out is definitely length. If length is a negative number, the subarray starts from offset and ends at count (target array) - |length| from the beginning of the array. Specifically, if length is empty, the end position is at the end of the array.

Example:
Copy code The code is as follows:

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

$output = array_slice($input, 2); // returns "c", "d" , and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a" , "b", and "c"

// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input , 2, -1, true));
?>

The above example will output:
Copy code The code is as follows:

Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)

2.array_splice()

carry The three parameters are the same as above, and their function is to delete the subarray with length starting from offset.

Example:
Copy code The code is as follows:

$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
// "blue", "black", "maroon")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
// "blue", "purple", "yellow");
?>


区别取值函数有四个:

1.array_intersect()

携带参数不定,均为数组,返回所有数组中公共元素的值组成的数组,数组的键由所在第一个数组的键给出。

例子:
复制代码 代码如下:

$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
?>

上例将输出:
复制代码 代码如下:

Array
(
[a] => green
[0] => red
)


2.array_intersect_assoc()

在前一个函数的基础上,返回所有数组中键、值均相同的键值对。

例子:
复制代码 代码如下:

$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result_array = array_intersect_assoc($array1, $array2);
?>

上例将输出:
复制代码 代码如下:

Array
(
[a] => green
)

3.array_diff()

携带多个数组,返回第一个数组中有的而后面的数组中没有的所有的值组成的新数组,对应键取自第一个数组。

例子:
复制代码 代码如下:

$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

上例将输出:
复制代码 代码如下:

Array
(
[1] => blue
)


4.array_diff_assoc()

在前一个函数的基础上,不仅需要匹配值还要匹配键。

例子:
复制代码 代码如下:

$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
?>

上例将输出:
复制代码 代码如下:

Array
(
[b] => brown
[c] => blue
[0] => red
)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321300.htmlTechArticleThere are three functions for merging arrays: 1.array_combine() carries two parameter arrays, and the value of parameter array one is new The key of the array, the value of parameter array two is used as the value of the new array. Very simple. Example: ...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles