PHP - Manual手册 - V. Array 数组函数 - array_merge合并一个或多个数组

PHP - Manual手册 - V. Array 数组函数 - array_merge合并一个或多个数组

array_merge

(PHP 4, PHP 5)

array_merge — 合并一个或多个数组

说明

array array_merge ( array $array1 [, array $array2 [, array $...]] )

array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。

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

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

例 237. array_merge() 例子

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

上例将输出:

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

例 238. 简单的 array_merge() 例子

<?php $array1 = array(); $array2 = array(1 => "data"); $result = array_merge($array1, $array2); ?>

别忘了数字键名将会被重新编号!

Array (     [0] => data )

如果你想完全保留原有数组并只想新的数组附加到后面,用 + 运算符:

<?php $array1 = array(); $array2 = array(1 => "data"); $result = $array1 + $array2; ?>

数字键名将被保留从而原来的关联保持不变。

Array (     [1] => data )
警告

array_merge() 的行为在 PHP 5 中被修改了。和 PHP 4 不同,array_merge() 现在只接受 array 类型的参数。不过可以用强制转换来合并其它类型。请看下面的例子。

例 239. array_merge() PHP 5 例子

<?php $beginning = 'foo'; $end = array(1 => 'bar'); $result = array_merge((array)$beginning, (array)$end); print_r($result); ?>

上例将输出:

Array (     [0] => foo     [1] => bar )

参见 array_merge_recursive()array_combine()数组运算符

[PHP: array_merge - Manual:]

http://www.php.net/manual/zh/function.array-merge.php

[PHP - 关键词]

php

[PHP - 相关论坛]

http://php.board.newsmth.net/ http://bbs.pku.edu.cn/, homepage看版 http://forum.csdn.net/SList/PHP/

[PHP - 下载]

AppServ 2.5.9, http://www.appservnetwork.com/

Zend Optimizer 3.3.0a, http://www.zend.com/products/zend_optimizer

phpMyAdmin 2.11.2, http://www.phpmyadmin.net/home_page/index.php

Zend Core 2.5.0, http://www.zend.com/products/zend_core

Zend Platform 3.0.3, http://www.zend.com/products/zend_platform

PHP 5.2.5, http://www.php.net/releases/5_2_5.php

Zend Studio 5.5, http://www.zend.com/products/zend_studio

Zend Guard 5.0, http://www.zend.com/products/zend_guard

[PHP - Manual手册]

PHP: PHP 类型比较表 - Manual: http://www.php.net/manual/zh/types.comparisons.php#id9240444

PHP: 变量范围 - Manual:, http://www.php.net/manual/zh/language.variables.scope.php#language.variables.scope.references

PHP - PHP 的命令行模式 - CLI 专用常量, http://www.php.net/manual/zh/features.commandline.php#id2715646

PHP - token_get_all用于解析PHP源代码, http://www.php.net/manual/zh/function.token-get-all.php

PHP - reset — 将数组的内部指针指向第一个单元, http://www.php.net/manual/zh/function.reset.php

PHP - serialize — 产生一个可存储的值的表示, http://www.php.net/manual/zh/function.serialize.php

PHP: 魔术常量 - Manual:, http://www.php.net/manual/zh/language.constants.predefined.php

PHP: SOAP Functions - soap.wsdl_cache_enabled, http://cn.php.net/manual/zh/ref.soap.php#ini.soap.wsdl-cache-enabled

字符串转换为数值, http://www.php.net/manual/zh/language.types.string.php#language.types.string.conversion

Class Abstraction类抽象, http://www.php.net/manual/zh/language.oop5.abstract.php

foreach遍历数组, http://www.php.net/manual/zh/control-structures.foreach.php

SOAP configuration options missing documentation文档中丢失SOAP配置选项, http://bugs.php.net/bug.php?id=38644

PHP 的命令行模式, http://www.php.net/manual/zh/features.commandline.php

位运算符, http://www.php.net/manual/zh/language.operators.bitwise.php#id2665181

file_exists检查文件或目录是否存在, http://www.php.net/manual/zh/function.file-exists.php

is_file判断给定文件名是否为一个正常的文件, http://www.php.net/manual/zh/function.is-file.php  

settype设置变量的类型, http://cn.php.net/manual/zh/function.settype.php#function.settype

is_numeric检测变量是否为数字或数字字符串, http://www.php.net/manual/zh/function.is-numeric.php

ksort对数组按照键名排序, http://www.php.net/manual/zh/function.ksort.php

[PHP - Manual手册 - 下载]

http://www.php.net/download-docs.php

[PHP - Manual手册 - 第19章 类与对象(PHP 5)]

Object cloning对象克隆, http://www.php.net/manual/zh/language.oop5.cloning.php

[PHP - Manual手册 - 第20章 异常处理]

概述, http://www.php.net/manual/zh/language.exceptions.php

用异常处理php改进流程

[PHP - Manual手册 - 第21章 引用的解释]

引用返回, http://www.php.net/manual/zh/language.references.return.php

[PHP - Manual手册 - V. Array 数组函数]

计算数组的交集, http://www.php.net/manual/zh/function.array-intersect.php

array_merge合并一个或多个数组, http://www.php.net/manual/zh/function.array-merge.php

[PHP - Manual手册 - XVIII. CURL, 客户端URL库函数]

概述, http://www.php.net/manual/zh/ref.curl.php

curl_setopt设置cURL传输的选项, http://www.php.net/manual/zh/function.curl-setopt.php#function.curl-setopt

[PHP - Manual手册 - XXII. Date/Time 日期/时间函数]

date格式化一个本地时间/日期, http://www.php.net/manual/zh/function.date.php

[PHP - Manual手册 - XXXIII. Error Handling and Logging Functions错误处理和日志函数]

概述, http://www.php.net/manual/zh/ref.errorfunc.php

[PHP - Manual手册 - XLVII. Function Handling Functions函数管理函数]

register_shutdown_function注册一个脚本结束时调用的函数, http://www.php.net/manual/zh/function.register-shutdown-function.php

[PHP - Manual手册 - XC. Miscellaneous Functions杂项函数]

sleep延迟执行, http://www.php.net/manual/zh/ref.misc.php

uniqid生成唯一ID, http://www.php.net/manual/zh/function.uniqid.php

[PHP - Manual手册 - XCVIII. MySQL 函数]

mysql_real_escape_string转义SQL语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集, http://www.php.net/manual/zh/function.mysql-real-escape-string.php   

[PHP - Manual手册 - CXXVII. PostgreSQL 数据库函数]

pg_insert将数组插入到表中, http://www.php.net/manual/zh/index.php

[PHP - Manual手册 - CXXX. Program Execution Functions程序调用函数]

proc_open执行命令并打开用于输入输出的文件指针, http://www.php.net/manual/zh/function.proc-open.php

[PHP - Manual手册 - CXLIX. Session 会话处理函数]

session.save_path 定义了传递给存储处理器的参数, http://www.php.net/manual/zh/ref.session.php#ini.session.save-path

[PHP - Manual手册 - CLXI. String 字符串处理函数]

str_pad使用另一个字符串将一个字符串填充到指定长度, http://www.php.net/manual/zh/function.str-pad.php

strpos查找一个字符串第一次出现的位置, http://www.php.net/manual/zh/function.strpos.php

substr返回字符串中的一部分, http://www.php.net/manual/zh/function.substr.php

[PHP - Manual手册 - CLXVI. Tidy Functions - Tidy函数]

概述, http://www.php.net/manual/zh/ref.tidy.php

[PHP - Manual手册 - CLXIX. URL 函数]

概述, http://www.php.net/manual/zh/ref.url.php

[PHP - Manual手册 - 附录 H. php.ini 配置选项]

register_globals决定是否将 EGPCS(Environment,GET,POST,Cookie,Server)变量注册为全局变量, http://www.php.net/manual/zh/ini.core.php#ini.register-globals

short_open_tag决定是否允许使用PHP代码开始标志的缩写形式, http://www.php.net/manual/zh/ini.core.php#ini.short-open-tag

[PHP - Manual手册 - 附录 L. 保留字列表]

服务器变量:$_SERVER, http://www.php.net/manual/zh/reserved.variables.php#reserved.variables.server

[PHP - 应用实例]

语法着色, php.exe -s "%1" > "%1.html"

移除SimpleXML对象中的子对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值