Home > Article > Backend Development > PHP method to merge two numeric key array values
This article mainly introduces the method of merging two numeric key array values in PHP, involving PHP's array traversal, judgment, conversion and other related operating skills. Friends in need can refer to the example of this article
Describes a simple method of merging two numeric key array values in PHP. Share it with everyone for your reference, as follows:
First you need to understand a basic knowledge point: analysis of the difference between PHP merging arrays 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 )
PHP Detailed explanation of the method of inserting elements before the specified Key in the associated array
PHP implementation to obtain array Method of single column value
php uses str_replace to replace multi-dimensional arrayDetailed implementation method
The above is the detailed content of PHP method to merge two numeric key array values. For more information, please follow other related articles on the PHP Chinese website!