Home > Backend Development > PHP Tutorial > Precautions for using the array_merge() function in php array merging_PHP tutorial

Precautions for using the array_merge() function in php array merging_PHP tutorial

WBOY
Release: 2016-07-13 10:27:36
Original
887 people have browsed it

   1.array_merge()合并

  例子

 代码如下  

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

 代码如下  

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

输出结果为

Array ( [a] => bb [b] => cc )

输出结果为

Array ( [a] => bb [b] => cc )

 代码如下  


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

  上面因为都是数组就没有问题了,假如我们把$array 设置不是数组看看什么情况
 代码如下  


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

Results after operation

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

Tell us that we must have an array, then I have many ways to solve this,

 1. Use is_array() to make a judgment, but you will find that if there are too many arrays to merge, the judgments one by one are unreasonable. Later, I found that the data type can be converted

The code is as follows

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

 代码如下  

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

输出结果不报错了

Array ( [0] => 1 [b] => cc )

The output results no longer report errors

Array ( [0] => 1 [b] => cc )

It automatically converts the number 1 into an array, so everyone must pay attention to these details when using it.

http://www.bkjia.com/PHPjc/816151.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/816151.htmlTechArticle
1.array_merge() merge example code is as follows $array = array('a'='bb'); $ array2 = array('b'='cc'); $array3 = array_merge($array,$array2); The output result is Array ( [a] = bb [b] = cc ) above...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template