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)
|