Home > Database > Mysql Tutorial > Detailed explanation of example of merging 2 numeric key array values ​​in PHP

Detailed explanation of example of merging 2 numeric key array values ​​in PHP

怪我咯
Release: 2017-06-16 11:15:02
Original
1362 people have browsed it

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(&#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);
Copy after login

Output:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template