Simple method of merging two arrays in PHP

Source: Internet
Author: User
Tags arrays pear

Array Merge Array_merge

Cases

The code is as follows Copy Code

$array 1 = array (1=> ' 0 ');
$array 2 = Array (1=> "data");
$result 3 = Array_merge ($array 2, $array 1),/* results are $array2 and $array1 values, the key name is reassigned * *

There is another way to use the "+" number directly

The code is as follows Copy Code

A = Array (
' A ',
);
$b = Array (
' U ',
);
$c = $a + $b;
Var_dump ($c);

Output
Array (1) {
[0]=>
String (1) "a"
}

Intersection of arrays

This section only introduces some functions, as the usage is very simple, do not want to say more, so as not to complicate the simple things.

Append array

Array array_merge_recursive (array Array1,array array2[...,array Arrayn])

The code is as follows Copy Code

<?php
$fruit 1 = Array ("Apple" => "red", "banana" => "yellow");
$fruit 2 = Array ("pear" => "yellow", "apple" => "green");
$result = array_merge_recursive ($fruit 1, $fruit 2);
Print_r ($result);

Output
Array ([Apple] => Array ([0] => red [1] => green) [Banana] => yellow [pear] => yellow)
?>

The difference set of associative arrays Array_diff () This is not an introduction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.