Maison > Article > développement back-end > array_merge函数的注意事项,array_merge函数_PHP教程
array_merge — 合并一个或多个数组
array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。
如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。
如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。
<span class="html">array_merge will return NULL if any of the arguments are NULL。</span>
例如:
$array1 = NULL;
$array2 = array(1 => "data");
$result = array_merge($array1, $array2);
var_dump($result);结果为NULL,所以在写SQL语句获取结果集的时候要注意,if(empty($resut)){$result=array();} 赋值为空数组后再和其他的数组进行合并。
例子:
<span>//</span><span>新的逻辑</span> <span>$agent_id</span>=<span>$location_model</span>->where("id='<span>$location_id</span>'")->getField('agent_id'<span>); </span><span>//</span><span>再查询已授权的运营商(要排除授权商家)</span> <span>if</span>(!<span>empty</span>(<span>$agent_id</span><span>)){ </span><span>$tpl_list2</span>=<span>$tpl_model</span>->where("status=1 and agent_range=2 and agent_id in (<span>$agent_id</span>) and supplier_id=''")->field(<span>$field</span>)->order('id desc')-><span>select(); } </span><span>if</span>(<span>empty</span>(<span>$tpl_list2</span><span>)){ </span><span>$tpl_list2</span>=<span>array</span><span>(); } </span><span>//</span><span>再查询授权全部运营商</span> <span>$tpl_list3</span>=<span>$tpl_model</span>->where("status=1 and agent_range=1")->field(<span>$field</span>)->order('id desc')-><span>select(); </span><span>if</span>(<span>empty</span>(<span>$tpl_list3</span><span>)){ </span><span>$tpl_list3</span>=<span>array</span><span>(); } <span>//<span>array_merge will return NULL if any of the arguments are NULL</span></span> </span><span>$tpl_list_merge</span>=<span>array_merge</span>(<span>$tpl_list1</span>,<span>$tpl_list2</span>,<span>$tpl_list3</span>);