Home  >  Article  >  Backend Development  >  PHP array_merge function characteristics in practice-one of the parameters is null

PHP array_merge function characteristics in practice-one of the parameters is null

WBOY
WBOYOriginal
2016-07-29 09:12:351440browse

When I was working on a project recently, I needed to merge two arrays read from the database, so I thought of using the array_merge function in PHP. The final result was always null. Through dump, I found that this was because one of the arrays was null. . The demonstration is as follows:

$arr1 =null;

$arr2 = array('tom','linken');

$arr3 = array_merge($arr1,$arr2);

var_dump( $arr3);

The result of the operation is null.

The solution is to cast the two parameters into array when merging;

as follows:

$arr1 =null;

$arr2 = array('tom',' linken');

$arr3 =array_merge((array)$arr1,(array)$arr2);

var_dump($arr3);

The operation result is:

array(2) { [0]=> ; string(3) "tom" [1]=> string(6) "linken" }

The problem is solved, so there is no need to judge whether $arr1 and $arr2 are null in the code.

The above has introduced the characteristics of the array_merge function in PHP practice - one of the parameters is null, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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