Considerations for using php combination and array_merge of function

  • 2021-07-02 23:39:08
  • OfStack

1. array_merge () Merge

Example


$array = array('a'=>'bb');
$array2 = array('b'=>'cc');
$array3 = array_merge($array,$array2);
 The output is 
Array ( [a] => bb [b] => cc )

There is no problem because all the above are arrays. If we set $array not to be arrays, we will see what happens


$array = 1;//array('a'=>'bb');
$array2 = array('b'=>'cc');
$array3 = array_merge($array,$array2);
print_r( $array3 );


Post-run results

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in E:test1.php on (www.ofstack.com)line 4

Tell us that we have to have an array, so I have many ways to solve this.

1. I used is_array () to judge, but I found that if there are more than one merged array, it is unreasonable to judge each one. Later, I found that I can convert the data type


$array = 1;//array('a'=>'bb');
$array2 = array('b'=>'cc');
$array3 = array_merge((array)$array,(array)$array2);
print_r( $array3 );
 No error is reported in the output result 
Array ( [0] => 1 [b] => cc )

He automatically converted the number 1 into an array, so everyone should pay attention to these details when using 1.


Related articles: