array_merge函数的注意事项

WBOY
Freigeben: 2016-07-29 09:10:31
Original
1280 Leute haben es durchsucht

array_merge — 合并一个或多个数组

array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。

  如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。

  如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引

<span>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>);
Nach dem Login kopieren

以上就介绍了array_merge函数的注意事项,包括了索引方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!