PHP Merge Array function array_merge ()

Source: Internet
Author: User

The Array_merge () function is a combination of arrays in PHP that can be used to synthesize an array and not change the value of the original array, but today I have a few small details when I use Array_merge to merge arrays, so let me give you some examples

1.array_merge () Merge

$array = Array (' A ' = ' BB '), $array 2 = array (' b ' + = ' cc '); $array 3 = Array_merge ($array, $array 2);

The output result is

Array ([A] = BB [b] = cc)


There is no problem with the above because it is an array, if we set the $array not an array to see what the situation

$array = 1;//array (' a ' = = ' BB '), $array 2 = array (' b ' + = ' cc '); $array 3 = Array_merge ($array, $array 2);p Rint_r ($array 3 );

Post-run results

Warning:array_merge () [Function.array-merge]: Argument #1 is not a array in e:test1.php on (www.jb51.net) line 4

Tell us it must be an array, then I have a number of ways to solve it,

1. Use Is_array () to judge, but will find that if the combined array of more than a reasonable judgment, and later found that can transform the data class

$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 does not have 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 them.

Combine two arrays into an array:

<?php$a1=array ("A" and "Red", "b" = "green"), $a 2=array ("c" = "Blue", "b" = "yellow");p Rint_r (Array_ Merge ($a 1, $a 2));? >

Definition and usage

The Array_merge () function is used to combine one or more arrays into an array.

Tip: You can enter one or more arrays into the function.

Note: If two or more array elements have the same key name, the last element overrides the other element.

Note: If you only enter an array into the Array_merge () function, and the key name is an integer, the function returns a new array with an integer key name, with the key name re-indexed starting at 0 (see Example 1 below).

Tip: The difference between this function and the array_merge_recursive () function is that it handles two or more arrays of elements with the same key name. Array_merge_recursive () does not overwrite the key name, but instead recursively composes the values of multiple identical key names into an array.

Grammar

Array_merge (Array1,array2,array3 ...)


Array1 required. Specifies the array.

Array2 is optional. Specifies the array.

Array3 is optional. Specifies the array.

Returns the merged array.

Use only one parameter with an integer key name:

<?php$a=array (3=> "Red",4=> "green");p Rint_r (Array_merge ($a));? 

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.