Table of Contents
重新认识php array_merge函数,phparraymerge
PHP中有没有合并数组值的函数(或者小技巧)
php的函数中&是什 如函数: private function test(&$array) {}
Home php教程 php手册 重新认识php array_merge函数,phparraymerge

重新认识php array_merge函数,phparraymerge

Jun 13, 2016 am 09:26 AM
array merge php private

重新认识php array_merge函数,phparraymerge

重新认识php array_merge函数

今天因一个Bug重新审视了下array_merge()这个函数。

定义:array_merge — 合并一个或多个数组

规范:array array_merge(array $array1 [, array $...])

说明:

1. 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。

2. 如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。   

3. 如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。

这个函数在手册中有详尽示例,用途也很广且实用。

今天遇到的一个问题是手册中有警告的,只是此前没注意到,导致了一个致命的错误。如下:

PHP >= 5.0 版本,array_merge() 只接受array类型的参数。不过可以用强制转换来合并其它类型。

对于是foreach等代码产生的数组变量要注意了,要么初始化该变量为空数组,要么就在合并时做个强制转换。不然,会有大苦头吃。因此,保持一个初始化变量的习惯也是个好事。

PHP中合并数组分成两种情况

1、如果这两个数组中有相同的字符串键名:

<&#63;php
 
 $book1 = array('linux'=>'linux服务器配置与管理','php'=>'PHP程序设计');
 $book2 = array('linux'=>'服务器配置与管理','jsp'=>'PHP'); 
 
 $result = array_merge($book1,$book2);
 print_r($result);
&#63;>
Copy after login

输出为:

Array ( [linux] => 服务器配置与管理 [php] => PHP程序设计 [jsp] => PHP )
Copy after login


说明,后者将替换前者。但如果使用的是array_merge_recursive()则可保留,并作一个子数组存在。如:

<&#63;php
 
 $book1 = array('linux'=>'linux服务器配置与管理','php'=>'PHP程序设计');
 $book2 = array('linux'=>'服务器配置与管理','jsp'=>'PHP'); 
 
 $result = array_merge_recursive($book1,$book2);
 print_r($result);
&#63;>
Copy after login

输出为:

Array ( [linux] => Array ( [0] => linux服务器配置与管理 [1] => 服务器配置与管理 ) [php] => PHP程序设计 [jsp] => PHP )
Copy after login


2、如果这两个数组中有相同的数值键名:

<&#63;php
 
 $book1 = array('linux服务器配置与管理','PHP程序设计');
 $book2 = array('服务器配置与管理','PHP'); 
 
 $result = array_merge($book1,$book2);
 print_r($result);
&#63;>
Copy after login

结果是:

Array ( [0] => linux服务器配置与管理 [1] => PHP程序设计 [2] => 服务器配置与管理 [3] => PHP )
Copy after login

 
这时,如果数组中包含相同的数字键名,则后面的不会覆盖前面的值,而是后面的键值按顺序依次增加,附在后边。明白了吗,^_^

PHP中有没有合并数组值的函数(或者小技巧)

1. 最简单的办法:使用 +

下列的代码:

$r1 = array('a'=>1,'b'=>3,'c'=>5,'d'=>7,'e'=>9);
$r2 = array('f'=>2,'g'=>4,'h'=>6,'i'=>8,'j'=>10);
$r = $r1+$r2;
echo '

';<br>  print_r($r);<br>  echo '';<br>?><br>代码输出结果:<br>Array<br>(<br>    [a] => 1<br>    [b] => 3<br>    [c] => 5<br>    [d] => 7<br>    [e] => 9<br>    [f] => 2<br>    [g] => 4<br>    [h] => 6<br>    [i] => 8<br>    [j] => 10<br>)<br><br>但这种办法有个需要注意的地方:两个关键字相同的元素会只保留一个,即$r1+r2,则$r1中的数据保留,这个在特殊场合也可能刚好用上也说不定。<br><br>2. 使用array_merge() 函数:<br><br><?php <br />$array1 = array("color" => "red", 2, 4);<br>$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);<br>$result = array_merge($array1, $array2);<br>print_r($result);<br>?><br>代码输出结果:<br>Array<br>(<br>    [color] => green<br>    [0] => 2<br>    [1] => 4<br>    [2] => a<br>    [3] => b<br>    [shape] => trapezoid<br>    [4] => 4<br>)<br>
  
<p class="header2"><span class="icon i-relatedanswer"><h3 id="php的函数中-amp-是什-如函数-private-function-test-amp-array">php的函数中&是什 如函数: private function test(&$array) {}</h3></span></p><p class="best-replyer"></p>
<p class="ft p1">这是函数的传址调用。$array是一个变量,传给test()的是$array的地址,函数直接改写$array的内容返回值<br>
  </p>
Copy after login
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

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 vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

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.

See all articles