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.