search
HomeBackend DevelopmentPHP Tutorialphp merge array function array_merge()
php merge array function array_merge()Nov 01, 2017 am 10:01 AM
arraymergephp

array_merge() function merges arrays in php. It can combine multiple arrays into one array without changing the original array. It’s worth it, but today I encountered several small details when using array_merge to merge arrays. Let me give you an example below

1.array_merge() merge

$array = array('a'=>'bb');
$array2 = array('b'=>'cc');
$array3 = array_merge($array,$array2);

The output result is

Array ( [a] => bb [b] => cc )


There is no problem because the above are all arrays. If we set $array to not be an array, let’s see what happens.

$array = 1;//array('a'=>'bb');
$array2 = array('b'=>'cc');
$array3 = array_merge($array,$array2);
print_r( $array3 );

After running The result

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in E:test1.php on (www.jb51.net)line 4

tells us that we must have an array, so I have many ways to solve this,

1. Use is_array() to judge, but you will find that if you merge the arrays More often than not, each judgment is unreasonable. Later, I found that the data class can be converted

$array = 1;//array('a'=>'bb');
$array2 = array('b'=>'cc');
$array3 = array_merge((array)$array,(array)$array2);
print_r( $array3 );
输出结果不报错了
Array ( [0] => 1 [b] => cc )


. It automatically converted the number 1 into an array, so everyone must pay attention to these when using it. Oh the details.

Merge two arrays into one array:

<?php$a1=array("a"=>"red","b"=>"green");$a2=array("c"=>"blue","b"=>"yellow");print_r(array_merge($a1,$a2));?>

Definition and usage

array_merge() function is used to merge one or more arrays into one array.

Tip: You can input one or more arrays to the function.

Note: If two or more array elements have the same key name, the last element will overwrite the other elements.

Note: If you only input an array to the array_merge() function, and the keys are integers, the function will return a new array with integer keys whose keys start with 0 starts reindexing (see Example 1 below).

Tip: The difference between this function and the array_merge_recursive() function is that it handles the case where two or more array elements have the same key name. array_merge_recursive() will not perform key name overwriting, but will recursively form multiple values ​​with the same key name into an array.

Syntax

array_merge(array1,array2,array3...)


array1 Required. Specifies an array.

array2 Optional. Specifies an array.

array3 Optional. Specifies an array.

Return the merged array.

Use only one parameter with an integer key name:

<?php$a=array(3=>"red",4=>"green");print_r(array_merge($a));?>

The above is the detailed content of php merge array function array_merge(). For more information, please follow other related articles on the PHP Chinese website!

Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

使用C#中的Array.Sort函数对数组进行排序使用C#中的Array.Sort函数对数组进行排序Nov 18, 2023 am 10:37 AM

标题:C#中使用Array.Sort函数对数组进行排序的示例正文:在C#中,数组是一种常用的数据结构,经常需要对数组进行排序操作。C#提供了Array类,其中有Sort方法可以方便地对数组进行排序。本文将演示如何使用C#中的Array.Sort函数对数组进行排序,并提供具体的代码示例。首先,我们需要了解一下Array.Sort函数的基本用法。Array.So

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.