Table of Contents
An issue you need to pay attention to when using the php array_merge function
Home Backend Development PHP Tutorial An issue you need to pay attention to when using the php array_merge function_PHP Tutorial

An issue you need to pay attention to when using the php array_merge function_PHP Tutorial

Jul 13, 2016 am 09:59 AM
array merge php use function Notice of question need

An issue you need to pay attention to when using the php array_merge function

This article mainly introduces an issue you need to pay attention to when using the php array_merge function. This article explains that array_merge will not merge numbers when merging arrays. Everyone needs to pay attention to the problem of key names when using it. Friends in need can refer to it

When using the array_merge function in PHP language, it is thought that the same key name will be overwritten, but please see the following code:

The code is as follows:


$a1 = array(1=>'abc', 3=>10);
$a2 = array(1=>'efg', 3=>20);
print_r(array_merge($a1, $a2));

What will be output? What we expected was:

The code is as follows:


Array
(
[1] => efg
[3] => 20
)

The actual output is:

The code is as follows:


Array
(
[0] => abc
[1] => 10
[2] => efg
[3] => 20
)

Not only was it not overwritten, but the numeric keys were re-indexed consecutively.

At first I thought this was a bug, then I read the php manual http://php.net/manual/zh/function.array-merge.php

「If the input array has the same string key name, the value after the key name will overwrite the previous value. However, if the array contains a numeric key name, the subsequent value will not overwrite the original value. Instead, append it to
. If only an array is given and the array is numerically indexed, the key names are re-indexed consecutively. 》

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/976534.htmlTechArticleA problem you need to pay attention to when using the php array_merge function. This article mainly introduces an issue you need to pay attention to when using the php array_merge function. , this article explains that array_merge will not merge when merging arrays...
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 Article Tags

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)

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 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

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

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

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

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles