Re understanding of php array_merge function

  • 2021-07-16 02:02:31
  • OfStack

Re-understanding of php array_merge function

Today, we re-examined the lower array_merge () function for an Bug.

Definition: array_merge--Merge 1 or more arrays

Specification: array array_merge (array $array1 [, array $...])

Description:

1. Combine the cells of one or more arrays, and append the values in one array after the previous one.

2. If the same string key name is entered in the array, the value following the key name overrides the first value. However, if the array contains numeric key names, the following values will not overwrite the original values, but will be appended to the following.

3. If only 1 array is given and the array is numerically indexed, the key names are re-indexed in a contiguous manner.

This function has detailed examples in the manual, and it is also very useful and practical.

A problem encountered today was warned in the manual, but it was not noticed before, which led to a fatal mistake. As follows:

PHP > = Version 5.0, array_merge () only accepts parameters of type array. However, you can use casts to merge other types.

For foreach and other code generated by the array variable should be noted, either initialize the variable as an empty array, or do a cast when merging. Otherwise, there will be great hardships. Therefore, it is also a good thing to keep the habit of 1 initialization variable.

Merging arrays in PHP can be divided into two cases

1. If both arrays have the same string key name:


<?php
 
 $book1 = array('linux'=>'linux Server configuration and management ','php'=>'PHP Programming ');
 $book2 = array('linux'=>' Server configuration and management ','jsp'=>'PHP'); 
 
 $result = array_merge($book1,$book2);
 print_r($result);
?>

The output is:


Array ( [linux] =>  Server configuration and management  [php] => PHP Programming  [jsp] => PHP )


Explain that the latter will replace the former. However, if array_merge_recursive () is used, it can be retained and exists as a subarray. Such as:


<?php
 
 $book1 = array('linux'=>'linux Server configuration and management ','php'=>'PHP Programming ');
 $book2 = array('linux'=>' Server configuration and management ','jsp'=>'PHP'); 
 
 $result = array_merge_recursive($book1,$book2);
 print_r($result);
?>

The output is:


Array ( [linux] => Array ( [0] => linux Server configuration and management  [1] =>  Server configuration and management  ) [php] => PHP Programming  [jsp] => PHP )


2. If both arrays have the same numeric key name:


<?php
 
 $book1 = array('linux Server configuration and management ','PHP Programming ');
 $book2 = array(' Server configuration and management ','PHP'); 
 
 $result = array_merge($book1,$book2);
 print_r($result);
?>

The result is:


Array ( [0] => linux Server configuration and management  [1] => PHP Programming  [2] =>  Server configuration and management  [3] => PHP )


At this point, if the array contains the same numeric key names, the following ones do not overwrite the previous values, but the following key values are incremented in sequence and appended to the following ones. Got it, ^ _ ^


Related articles: