Home Backend Development PHP Tutorial Two methods of PHP array fusion

Two methods of PHP array fusion

Jun 09, 2018 pm 01:51 PM
array_merge php the difference Merge arrays

This article mainly introduces two methods of PHP array fusion. Interested friends can refer to it. I hope it will be helpful to everyone.

There are generally two methods of array fusion in PHP. One is to add directly using the plus sign, and the other is to use the array_merge function to add. There is a slight difference between the two:

1. Addition will ensure that the natural index in the array will not be reset
2. In the addition method, the value in the added array will not be overwritten
3. The natural index in the merge function will be reset Reset
4. The merge function does not matter the relationship between merged and merged. The value of the array parameter at the back will overwrite the value of the same key of the array parameter at the front.

Example:

Array 1:

$arr1 = array(
  10 => 'valueof10',
  11 => 'valueof11',
  'key1' => 'dataofkey1',
  'key2' => 'dataofkey2',
  'array' => array(
    'key1' => 1,
    'key2' => 'abc',
    'key3' => array(
      1, 2, 'name' => 'peter'
    ),
  ),
);
Copy after login

Array 2:

$arr2 = array(
  10 => 'newvalueof10',
  11 => 'newvalueof11',
  'key1' => 'newdataofkey1',
  'key3' => 'newdataofkey3',
  'array' => array(
    'key1' => 2,
    'key2' => 'defg',
    'key3' => array(
      1, 2, 'name' => 'jonathan', 'gender' => 'male'
    ),
  ),
);
Copy after login

Result comparison:

array (size=6)
 10 => string 'valueof10' (length=9)
 11 => string 'valueof11' (length=9)
 'key1' => string 'dataofkey1' (length=10)
 'key2' => string 'dataofkey2' (length=10)
 'array' => 
  array (size=3)
   'key1' => int 1
   'key2' => string 'abc' (length=3)
   'key3' => 
    array (size=3)
     0 => int 1
     1 => int 2
     'name' => string 'peter' (length=5)
 'key3' => string 'newdataofkey3' (length=13)
 
array (size=8)
 0 => string 'valueof10' (length=9)
 1 => string 'valueof11' (length=9)
 'key1' => string 'newdataofkey1' (length=13)
 'key2' => string 'dataofkey2' (length=10)
 'array' => 
  array (size=3)
   'key1' => int 2
   'key2' => string 'defg' (length=4)
   'key3' => 
    array (size=4)
     0 => int 1
     1 => int 2
     'name' => string 'jonathan' (length=8)
     'gender' => string 'male' (length=4)
 2 => string 'newvalueof10' (length=12)
 3 => string 'newvalueof11' (length=12)
 'key3' => string 'newdataofkey3' (length=13)
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php operations for reading, editing and saving files

php utilization Regular expression to extract links in content

How does php convert numbers into Chinese characters

The above is the detailed content of Two methods of PHP array fusion. For more information, please follow other related articles on the PHP Chinese website!

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

Does Bitcoin have stocks? Does Bitcoin have equity? Does Bitcoin have stocks? Does Bitcoin have equity? Mar 03, 2025 pm 06:42 PM

Does Bitcoin have stocks? Does Bitcoin have equity?

deepseek What is the difference between r1 and v3 version deepseek What is the difference between r1 and v3 version Feb 19, 2025 pm 03:24 PM

deepseek What is the difference between r1 and v3 version

Summary of FAQs for DeepSeek usage Summary of FAQs for DeepSeek usage Feb 19, 2025 pm 03:45 PM

Summary of FAQs for DeepSeek usage

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

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

PHP Program to Count Vowels in a String

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

How do you parse and process HTML/XML in PHP?

What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading Mar 03, 2025 pm 11:54 PM

What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading

See all articles