How PHP uses Array_merge to rearrange array subscripts _php tips

Source: Internet
Author: User

This example describes how PHP uses Array_merge to rearrange the array subscripts. Share to everyone for your reference. Specifically as follows:

Using a array_unique to remove the duplicates inside an array, but found that the subscript retains the subscript of the original array, but PHP uses a for loop that needs to be labeled neatly, so looking for a way to rearrange the subscripts of the array array_merge can solve this problem

The Array_merge () function merges two or more arrays into an array.

If the key name is duplicated, the key value of the key is the value of the last key name (preceded by the overlay). If the array is a numeric index, the key name is sequentially indexed.

Note: If you simply enter an array to the Array_merge () function and the key is an integer, the function returns a new array with the integer key name, whose key name is indexed in 0. (see Example 2)

Grammar:

Array_merge (Array1,array2,array3 ...)

Parameters Description
Array1 Necessary. The first array entered.
Array2 Necessary. The second array entered.
Array3 Optional. Multiple input arrays that can be specified.

Example 1

<?php
$a 1=array ("a" => "horse", "B" => "Dog");
$a 2=array ("C" => "Cow", "B" => "Cat");
Print_r (Array_merge ($a 1, $a 2));
? >

Output:

Array ([a] => horse [b] => Cat [c] => Cow)

Example 2

Use only one array parameter:

<?php
$a =array (3=> "horse",4=> "Dog");
Print_r (Array_merge ($a));
>

Output:

Array ([0] => horse [1] => Dog)

I hope this article will help you with your PHP program design.

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.