Home > Backend Development > PHP Problem > Use the array_merge() function to integrate arrays in php

Use the array_merge() function to integrate arrays in php

autoload
Release: 2023-03-09 12:44:01
Original
2468 people have browsed it

Use the array_merge() function to integrate arrays in php

In actual use, we may need to merge one or more arrays into one array. At this time, we may need to use array_merge()Function, this article will take you to take a look. First of all, we must take a look at this function syntax:

array_merge    ( array $a ,array $b,......  )
Copy after login
  • $a,$b,... : Array to be merged

  • Return value: merged array

Code example:

1. Use the array_merge() function

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Copy after login
输出:Array
([color] => green[0] => 2[1] => 4[2] => a[3] => b[shape] => trapezoid [4] => 4)
Copy after login

2. Use " " to connect

<?php
$array1 = array(0 => &#39;zero_a&#39;, 2 => &#39;two_a&#39;, 3 => &#39;three_a&#39;);
$array2 = array(1 => &#39;one_b&#39;, 3 => &#39;three_b&#39;, 4 => &#39;four_b&#39;);
$result = $array1 + $array2;
var_dump($result);
?>
Copy after login
输出:
array(5) { [0]=> string(6) "zero_a" [2]=> string(5) "two_a" [3]=> string(7) "three_a" [1]=> string(5) "one_b" [4]=> string(6) "four_b" }
Copy after login

3. Combine non-array classes

<?php
$beginning = &#39;foo&#39;;
$end = array(1 => &#39;bar&#39;);
$result = array_merge((array)$beginning, (array)$end);
print_r($result);
?>
Copy after login
输出:Array ( [0] => foo [1] => bar )
Copy after login

Recommended: 2021 PHP Interview Questions Summary (Collection)》《 php video tutorial

The above is the detailed content of Use the array_merge() function to integrate arrays 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