Home >Database >Mysql Tutorial >Detailed explanation of example of merging 2 numeric key array values in PHP
This article mainly introduces PHP's simple method of merging two numeric key array values, involving PHP's array traversal, judgment, conversion and other related operating skills. Friends in need can refer to this article
The example describes a simple method of merging two numeric key array values in PHP. Share it with everyone for your reference, the details are as follows:
You must first understand a basic knowledge point: Analysis of the difference between PHP merging array + and array_merge
<?php /** * PHP合并2个数字键数组的值 * * @param array $arr1 * @param array $arr2 * @return array */ function new_array_merge($arr1, $arr2) { if (!is_array($arr1) || !is_array($arr2)) { return array(); } $arr = array_flip($arr1) + array_flip($arr2); return array_keys($arr); } $arr1 = array('aa', 'bb', 'cc'); $arr2 = array('aa2', 'bb', 'cc2'); $arr = new_array_merge($arr1, $arr2); print_r($arr);
Output:
Array ( [0] => aa [1] => bb [2] => cc [3] => aa2 [4] => cc2 )
The above is the detailed content of Detailed explanation of example of merging 2 numeric key array values in PHP. For more information, please follow other related articles on the PHP Chinese website!