Example of an array merge append array in PHP

Source: Internet
Author: User
Tags arrays pear

PHP Merge Array We can use the Array_merge () function, and the Array_merge () function returns a federated array. The resulting array begins with the first input array parameter, appended in the order in which the following array parameters appear. In the form of:

Array Array_merge (array array1 array2...,arrayn)

The following is an example of a php merge array:

The code is as follows Copy Code

<?php
$fruits = Array ("Apple", "banana", "pear");
$numbered = Array ("1", "2", "3");
$cards = Array_merge ($fruits, $numbered);
Print_r ($cards);
Output results:
Array ([0] => Apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3)
?>

Append the array with PHP, use array_merge_recursive (), combine two numbers together, note that the Array_merge () function is not the same, Array_merge () of the two array with duplicates will be overwritten, and Array_ Merge_recursive () is not. Array_merge_recursive () Syntax:


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

The following is an example of a PHP append array:

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 results:
Array ([Apple] => Array ([0] => red [1] => green) [Banana] => yellow [pear] => yellow)
?>

Now Apple points to an array of indexed arrays of two color values, and there's a strange way to merge arrays

The Array_splice () syntax format is:
Array array_splice (array array, int Offset[,length[,array replacement]])

Example

The code is as follows Copy Code

<?php
$fruits = Array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "watermelon");
$subset = Array_splice ($fruits, 4);
Print_r ($fruits);
Print_r ($subset);
The output results are:
Array ([0] => Apple [1] => Banana [2] => Orange [3] => Pear)
Array ([0] => Grape [1] => Lemon [2] => watermelon)
?>

We can also use optional parameter replacement to specify an array to replace the target part. Take a look at the following example:

  code is as follows copy code

<?php
$fruits = Array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "watermelon");
$subset = Array_splice ($fruits, 2,-1, Array ("Green Apple", "Red Apple"));
Print_r ($fruits);
Print_r ($subset);
//output:
//Array ([0] => Apple [1] => Banana [2] => Green Apple [3] => Red Apple [4] => Watermel ON)
//Array ([0] => Orange [1] => Pear [2] => Grape [3] => Lemon)

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.