Home php教程 php手册 PHP数组合并:[“+”运算符]、[array_merge]、[array_merge_recursive]区别

PHP数组合并:[“+”运算符]、[array_merge]、[array_merge_recursive]区别

Jun 14, 2016 am 12:02 AM
array merge php array code Open source programming programming language software development operator

1、“+”运算符
规则:
  当两个数组的键名是数字键名或者字符串键名
  $c = $a + $b
  在$a后追加($b在$a中不存在的键名)键名和值
注意:
  1、不覆盖,只是追加不存在的键名和对应的值
  2、键名不重新索引
  3、无论是全部数字键名还是混合,都只是追加键名和值,如果键名相同则不进行追加,即把最先出现的值作为最终结果返回
例1:数字键名

$a = array(
    'a',
);
$b = array(
    'u',
);
$c = $a + $b;
var_dump($c);

output:
array(1) {
  [0]=>
  string(1) "a"
}
Copy after login

 例2:数字键名

$a = array(
    66=>'a',
);
$b = array(
    60=>'u',
    66=>'c'
);
$c = $a + $b;
var_dump($c);

output:
array(2) {
  [66]=>
  string(1) "a"
  [60]=>
  string(1) "u"
}
Copy after login

例3:字符键名

<?php  
$a = array(
    1=>'a',
    2=>'b',
    'c'=>'c',
    'd'=>'d',
);
$b = array(
    1=>'u',
    3=>'v',
    'c'=>'w',
    'd'=>'x',
    'y'=>'y',
    60=>'z',
);
$c = $a + $b;
var_dump($c);
?>

output:
array(7) {
  [1]=>
  string(1) "a"
  [2]=>
  string(1) "b"
  ["c"]=>
  string(1) "c"
  ["d"]=>
  string(1) "d"
  [3]=>
  string(1) "v"
  ["y"]=>
  string(1) "y"
  [60]=>
  string(1) "z"
}
Copy after login

 
2、array array_merge ( array array1 [, array array2 [, array ...]] )
规则:
  array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。
  如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。
  然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。
  如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。
注意:
  1、数字索引,不会覆盖,值合并后,键名会连续方式重新索引
  2、字符串键名,则该键名后面的值将覆盖前一个值
例1:数字键名

$a = array(
    'a',
);
$b = array(
    'u',
);
$c = array_merge($a, $b);
var_dump($c);

output:
array(2) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "u"
}
Copy after login

 例2:数字键名

$a = array(
    66=>'a',
);
$b = array(
    60=>'u',
    66=>'c'
);
$c = array_merge($a, $b);
var_dump($c);

output:
array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "u"
  [2]=>
  string(1) "c"
}
Copy after login

例3:字符键名

$a = array(
    1=>'a',
    2=>'b',
    'c'=>'c',
    'd'=>'d',
);
$b = array(
    1=>'u',
    3=>'v',
    'c'=>'w',
    'd'=>'x',
    'y'=>'y',
    60=>'z',
);
$c = array_merge($a, $b);
var_dump($c);

output:
array(8) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  ["c"]=>
  string(1) "w"
  ["d"]=>
  string(1) "x"
  [2]=>
  string(1) "u"
  [3]=>
  string(1) "v"
  ["y"]=>
  string(1) "y"
  [4]=>
  string(1) "z"
}
Copy after login

 

3、array array_merge_recursive ( array array1 [, array ...] )
  array_merge_recursive() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。
  如果输入的数组中有相同的字符串键名,则这些值会被合并到一个数组中去,这将递归下去,因此如果一个值本身是一个数组,本函数将按照相应的条目把它合并为另一个数组。
  然而,如果数组具有相同的数组键名,后一个值将不会覆盖原来的值,而是附加到后面。
注意:
  1、规则跟array_merge基本相同,只是在处理相同字符键名的时候,采用递归追加
例1:数字键名

$a = array(
    'a',
);
$b = array(
    'u',
);
$c = array_merge_recursive($a, $b);
var_dump($c);

output:
array(2) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "u"
}
Copy after login
Copy after login

例2:数字键名

$a = array(
    'a',
);
$b = array(
    'u',
);
$c = array_merge_recursive($a, $b);
var_dump($c);

output:
array(2) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "u"
}
Copy after login
Copy after login

例3:字符键名

$a = array(
    1=>'a',
    2=>'b',
    'c'=>'c',
    'd'=>'d',
);
$b = array(
    1=>'u',
    3=>'v',
    'c'=>'w',
    'd'=>'x',
    'y'=>'y',
    60=>'z',
);
$c = array_merge_recursive($a, $b);
var_dump($c);

output:
array(8) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  ["c"]=>
  array(2) {
    [0]=>
    string(1) "c"
    [1]=>
    string(1) "w"
  }
  ["d"]=>
  array(2) {
    [0]=>
    string(1) "d"
    [1]=>
    string(1) "x"
  }
  [2]=>
  string(1) "u"
  [3]=>
  string(1) "v"
  ["y"]=>
  string(1) "y"
  [4]=>
  string(1) "z"
}
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 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)

Huawei's official introductory tutorial for Cangjie programming language is released. Learn how to obtain the universal version SDK in one article Huawei's official introductory tutorial for Cangjie programming language is released. Learn how to obtain the universal version SDK in one article Jun 25, 2024 am 08:05 AM

According to news from this site on June 24, at the keynote speech of the HDC2024 Huawei Developer Conference on June 21, Gong Ti, President of Huawei Terminal BG Software Department, officially announced Huawei’s self-developed Cangjie programming language. This language has been developed for 5 years and is now available for developer preview. Huawei's official developer website has now launched the official introductory tutorial video of Cangjie programming language to facilitate developers to get started and understand it. This tutorial will take users to experience Cangjie, learn Cangjie, and apply Cangjie, including using Cangjie language to estimate pi, calculate the stem and branch rules for each month of 2024, see N ways of expressing binary trees in Cangjie language, and use enumeration types to implement Algebraic calculations, signal system simulation using interfaces and extensions, and new syntax using Cangjie macros, etc. This site has tutorial access address: ht

After 5 years of research and development, Huawei's next-generation programming language 'Cangjie” has officially launched its preview After 5 years of research and development, Huawei's next-generation programming language 'Cangjie” has officially launched its preview Jun 22, 2024 am 09:54 AM

This site reported on June 21 that at the HDC2024 Huawei Developer Conference this afternoon, Gong Ti, President of Huawei Terminal BG Software Department, officially announced Huawei’s self-developed Cangjie programming language and released a developer preview version of HarmonyOSNEXT Cangjie language. This is the first time Huawei has publicly released the Cangjie programming language. Gong Ti said: "In 2019, the Cangjie programming language project was born at Huawei. After 5 years of R&D accumulation and heavy R&D investment, it finally meets global developers today. Cangjie programming language integrates modern language features, comprehensive compilation optimization and Runtime implementation and out-of-the-box IDE tool chain support create a friendly development experience and excellent program performance for developers. "According to reports, Cangjie programming language is an all-scenario intelligence tool.

Huawei launches HarmonyOS NEXT Cangjie programming language developer preview beta recruitment Huawei launches HarmonyOS NEXT Cangjie programming language developer preview beta recruitment Jun 22, 2024 am 04:07 AM

According to news from this site on June 21, Huawei’s self-developed Cangjie programming language was officially unveiled today, and the official announced the launch of HarmonyOSNEXT Cangjie language developer preview version Beta recruitment. This upgrade is an early adopter upgrade to the developer preview version, which provides Cangjie language SDK, developer guides and related DevEcoStudio plug-ins for developers to use Cangjie language to develop, debug and run HarmonyOSNext applications. Registration period: June 21, 2024 - October 21, 2024 Application requirements: This HarmonyOSNEXT Cangjie Language Developer Preview Beta recruitment event is only open to the following developers: 1) Real names have been completed in the Huawei Developer Alliance Certification; 2) Complete H

Tianjin University and Beihang University are deeply involved in Huawei's 'Cangjie” project and launched the first AI agent programming framework 'Cangqiong” based on domestic programming languages. Tianjin University and Beihang University are deeply involved in Huawei's 'Cangjie” project and launched the first AI agent programming framework 'Cangqiong” based on domestic programming languages. Jun 23, 2024 am 08:37 AM

According to news from this site on June 22, Huawei yesterday introduced Huawei’s self-developed programming language-Cangjie to developers around the world. This is the first public appearance of Cangjie programming language. According to inquiries on this site, Tianjin University and Beijing University of Aeronautics and Astronautics were deeply involved in the research and development of Huawei’s “Cangjie”. Tianjin University: Cangjie Programming Language Compiler The software engineering team of the Department of Intelligence and Computing of Tianjin University joined hands with the Huawei Cangjie team to deeply participate in the quality assurance research of the Cangjie programming language compiler. According to reports, the Cangjie compiler is the basic software that is symbiotic with the Cangjie programming language. In the preparatory stage of the Cangjie programming language, a high-quality compiler that matches it became one of the core goals. As the Cangjie programming language evolves, the Cangjie compiler is constantly being upgraded and improved. In the past five years, Tianjin University

Huawei's self-developed Cangjie programming language official website and development documents are online, integrating into the Hongmeng ecosystem for the first time Huawei's self-developed Cangjie programming language official website and development documents are online, integrating into the Hongmeng ecosystem for the first time Jun 22, 2024 am 03:10 AM

According to news from this site on June 21, before the HDC2024 Huawei Developer Conference, Huawei’s self-developed Cangjie programming language was officially unveiled, and the Cangjie official website is now online. The official website introduction shows that Cangjie programming language is a new generation programming language for all-scenario intelligence, focusing on "native intelligence, natural all-scenarios, high performance, and strong security." Integrate into the Hongmeng ecosystem to provide developers with a good programming experience. The official website attached to this site introduces as follows: Native intelligent programming framework embedded with AgentDSL, organic integration of natural language & programming language; multi-Agent collaboration, simplified symbolic expression, free combination of patterns, supporting the development of various intelligent applications. Innately lightweight and scalable runtime for all scenes, modular layered design, no matter how small the memory is, it can be accommodated; all-scenario domain expansion

The last link of Huawei's pure-blood Hongmeng ecosystem! Self-developed Cangjie programming language will make its debut The last link of Huawei's pure-blood Hongmeng ecosystem! Self-developed Cangjie programming language will make its debut Jun 21, 2024 pm 03:23 PM

According to news on June 21, this afternoon, Huawei Developer Conference 2024 will be officially opened. "Pure-blood Hongmeng" Harmony OS NEXT is naturally a top priority. According to the plan previously revealed by Yu Chengdong, the public beta may be officially announced this afternoon, and ordinary consumers can also try out "pure-blood Harmony". According to reports, the first batch of supported mobile phones are the Mate60 series and Pura70 series. It is worth noting that as a "pure-blooded Hongmeng", HarmonyOSNEXT has removed the traditional Linux kernel and AOSP Android open source code and developed the entire stack in-house. According to the latest report from Sina Technology, Huawei will also complete the last link of Hongmeng Ecosystem and expand its presence in the world.

Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Jun 12, 2024 pm 08:38 PM

Since the launch of ChatGLM-6B on March 14, 2023, the GLM series models have received widespread attention and recognition. Especially after ChatGLM3-6B was open sourced, developers are full of expectations for the fourth-generation model launched by Zhipu AI. This expectation has finally been fully satisfied with the release of GLM-4-9B. The birth of GLM-4-9B In order to give small models (10B and below) more powerful capabilities, the GLM technical team launched this new fourth-generation GLM series open source model: GLM-4-9B after nearly half a year of exploration. This model greatly compresses the model size while ensuring accuracy, and has faster inference speed and higher efficiency. The GLM technical team’s exploration has not

Huawei: Cangjie programming language is independently controllable and does not evolve based on any existing programming language Huawei: Cangjie programming language is independently controllable and does not evolve based on any existing programming language Jun 22, 2024 pm 12:26 PM

According to news from this site on June 21, Huawei’s self-developed Cangjie programming language was officially unveiled today, and the HarmonyOSNEXT Cangjie language developer preview version Beta recruitment was launched. The Cangjie Programming Language Q&A page on Huawei's official website shows that Cangjie Programming Language is a next-generation application programming language oriented to all-scenario intelligence. It focuses on native intelligence, natural all-scenarios, high performance and strong security. It is combined with Hongmeng system to provide good programming. experience. For different business scenarios, Hongmeng Ecosystem provides application developers with multi-language hybrid development capabilities such as Cangjie and ArkTS. Cangjie and ArkTS develop together and form complementary advantages in the Hongmeng ecosystem. Cangjie is more suitable for business scenarios with high performance and high concurrency requirements. The goal of Cangjie programming language is to create Hongmeng applications that can perform tasks concurrently.

See all articles