Home >Backend Development >PHP Tutorial >PHP method to merge two numeric key array values

PHP method to merge two numeric key array values

墨辰丷
墨辰丷Original
2018-05-22 09:40:251126browse

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(&#39;aa&#39;, &#39;bb&#39;, &#39;cc&#39;);
$arr2 = array(&#39;aa2&#39;, &#39;bb&#39;, &#39;cc2&#39;);
$arr = new_array_merge($arr1, $arr2);
print_r($arr);


Output:


Array
(
  [0] => aa
  [1] => bb
  [2] => cc
  [3] => aa2
  [4] => cc2
)



##Related recommendations:

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!

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