PHP Array Merge Array

Source: Internet
Author: User
Tags arrays

1.array_merge () Merge

Example

The code is as follows

$array = Array (' A ' => ' BB ');
$array 2 = Array (' B ' => ' cc ');
$array 3 = Array_merge ($array, $array 2);

Output results are

Array ([a] => BB [b] => cc)

It's all right because it's all arrays, and if we're going to set the $array not an array, see what happens.

The code is as follows


$array = 1;//array (' A ' => ' BB ');
$array 2 = Array (' B ' => ' cc ');
$array 3 = Array_merge ($array, $array 2);
Print_r ($array 3);

Post-run results

Warning:array_merge () [Function.array-merge]: Argument #1 is not ' an array in e:test1.php ' line 4

Tell us that we have to have an array, then I have a number of ways to solve it,

1. Use Is_array () to judge, but found that if the combination of arrays more than a reasonable judge, later found that you can convert the data type

The code is as follows

$array = 1;//array (' A ' => ' BB ');
$array 2 = Array (' B ' => ' cc ');
$array 3 = Array_merge ((array) $array, (array) $array 2);
Print_r ($array 3);

The output is not an error

Array ([0] => 1 [b] => cc)

He automatically converts the number 1 into an array, so be sure to pay attention to these details when you use it.

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.