Precautions for the array_merge function

Source: Internet
Author: User
: This article mainly introduces considerations for the array_merge function. if you are interested in the PHP Tutorial, please refer to it. Array_merge-merge one or more arrays

Array_merge () combines the units of one or more arrays. the values in an array are appended to the values of the previous array. Returns an array of results.

If the input array contains the same string key name, the value after the key name overwrites the previous value. However, if the array contains a number key nameNoOverwrite the original value, but append it to the end.

If only one array is given and the array is a numberIndex, The key name will be reconnected in a continuous mannerIndex.

array_merge will return NULL if any of the arguments are NULL。

For example:

$ Array1 = NULL;
$ Array2 = array (1 => "data ");
$ Result = array_merge ($ array1, $ array2 );
Var_dump ($ result); the result is NULL. Therefore, when writing an SQL statement to obtain the result set, note that if (empty ($ resut) {$ result = array ();} the value is an empty array and then merged with other arrays.

Example:

// New Logic $ agent_id = $ location_model-> where ("id = '$ location_id'")-> getField ('Agent _ id '); // query the authorized carrier (to exclude authorized merchants) if (! Empty ($ agent_id) {$ tpl_list2 = $ tpl_model-> where ("status = 1 and agent_range = 2 and agent_id in ($ agent_id) and supplier_id = ''") -> field ($ field)-> order ('Id desc')-> select ();} if (empty ($ tpl_list2) {$ tpl_list2 = array ();} // query and authorize all carriers $ tpl_list3 = $ tpl_model-> where ("status = 1 and agent_range = 1")-> field ($ field) -> order ('Id desc ')-> select (); if (empty ($ tpl_list3) {$ tpl_list3 = array ();} // array_merge will return NULL if any of the arguments are NULL $ tpl_list_merge = array_merge ($ tpl_list1, $ tpl_list2, $ tpl_list3 );

The above describes the precautions for the array_merge function, including the index content, and hope to help those who are interested in the PHP Tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.