Home  >  Article  >  Backend Development  >  Notes on array_merge function

Notes on array_merge function

WBOY
WBOYOriginal
2016-07-29 09:10:311316browse

array_merge — Merge one or more arrays

array_merge() Merges the cells of one or more arrays, with the values ​​in one array appended to the previous array. Returns the resulting array.

 If the input array has the same string key name, the value after the key name will overwrite the previous value. However, if the array contains numeric key names, the subsequent value will not overwrite the original value, but will be appended to it.  If only one array is given and the array is numerically

indexed

, the key names will be reindexed in a continuous manner.

array_merge will return NULL if any of the arguments are NULL. For example: <span></span> result) ;The result is NULL, so when writing SQL statements to obtain the result set, pay attention to if(empty($resut)){$result=array();} assign an empty array and then merge it with other arrays.

<span></span>

Example:


<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>);

The above introduces the precautions for the array_merge function, including indexing. I hope it will be helpful to friends who are interested in PHP tutorials.

<span></span>

Statement:
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