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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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.

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 Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

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

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 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.

See all articles