PHP array_merge() 函数
array_merge() 函数用于合并两个或多个数组,它返回一个合并元素的新数组。
用法:
array_merge(array1, array2,...);
它接受两个或多个数组并返回一个包含合并元素的新数组。
例子:
Input: $arr1 = array(10, 20, 30, 40, 50); $arr2 = array(60, 70, 80, 90, 10); Output: Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 [5] => 60 [6] => 70 [7] => 80 [8] => 90 [9] => 10 )
PHP 代码 1:合并两个索引数组
<?php
$arr1 = array(10, 20, 30, 40, 50);
$arr2 = array(60, 70, 80, 90, 10);
//merging arrays
$arr3 = array_merge($arr1, $arr2);
//printing
print_r ($arr3);
?>
输出
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 [5] => 60 [6] => 70 [7] => 80 [8] => 90 [9] => 10 )
PHP 代码 2:合并两个具有不同键和值的关联数组
<?php
$arr1 = array("a" => "Hello", "b" => "Hi");
$arr2 = array("c" => "Bye!", "d" => "Nothing");
//merging arrays
$arr3 = array_merge($arr1, $arr2);
//printing
print_r ($arr3);
?>
输出
Array ( [a] => Hello [b] => Hi [c] => Bye! [d] => Nothing )
PHP 代码 3:合并两个具有不同键和重复值的关联数组
<?php
$arr1 = array("a" => "Hello", "b" => "Hi");
$arr2 = array("c" => "Hello", "d" => "Hi");
//merging arrays
$arr3 = array_merge($arr1, $arr2);
//printing
print_r ($arr3);
?>
输出
Array ( [a] => Hello [b] => Hi [c] => Hello [d] => Hi )
PHP 代码 4:合并两个具有重复键和唯一值的关联数组
<?php
$arr1 = array("a" => "Hello", "b" => "Hi");
$arr2 = array("a" => "Okay!", "d" => "Nothing");
//merging arrays
$arr3 = array_merge($arr1, $arr2);
//printing
print_r ($arr3);
?>
输出
Array ( [a] => Okay! [b] => Hi [d] => Nothing )
PHP 代码 5:合并索引数组和关联数组
<?php
$arr1 = array(10, 20, 30, 40);
$arr2 = array("c" => "Hello", "d" => "Hi");
//merging arrays
$arr3 = array_merge($arr1, $arr2);
//printing
print_r ($arr3);
?>
输出
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [c] => Hello [d] => Hi )
相关用法
- PHP array_merge_recursive()用法及代码示例
- PHP array_multisort()用法及代码示例
- PHP array_map()用法及代码示例
- PHP array_reverse()用法及代码示例
- PHP array_filter()用法及代码示例
- PHP array_pop()用法及代码示例
- PHP array_diff_uassoc()用法及代码示例
- PHP array_intersect_key()用法及代码示例
- PHP array_key_first()用法及代码示例
- PHP array_walk_recursive()用法及代码示例
- PHP array_shift()用法及代码示例
- PHP array_splice()用法及代码示例
- PHP array_search()用法及代码示例
- PHP array_intersect_assoc()用法及代码示例
- PHP array_sum()用法及代码示例
- PHP array_walk()用法及代码示例
- PHP array_diff_ukey()用法及代码示例
- PHP array_key_last()用法及代码示例
- PHP array_count()用法及代码示例
- PHP array_fill_keys()用法及代码示例
注:本文由纯净天空筛选整理自 PHP array_merge() function with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。