PHP array of merge split distinguish value function set

  • 2020-03-31 20:19:41
  • OfStack

Merging an array has three functions:

1. The array_combine ()

Carries two parameter arrays, the value of parameter array one as the key of the new array, and the value of parameter array two as the value of the new array. Very simple.

Example:
 
<?php 
$a = array('green', 'red', 'yellow'); 
$b = array('avocado', 'apple', 'banana'); 
$c = array_combine($a, $b); 

print_r($c); 
?> 

The above example will output:
 
Array 
( 
[green] => avocado 
[red] => apple 
[yellow] => banana 
) 


2. The array_merge ()

Take two argument arrays and simply append array 2 to the end of array 1 to form a new array.

Example:
 
<?php 
$array1 = array("color" => "red", 2, 4); 
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); 
$result = array_merge($array1, $array2); 
print_r($result); 
?> 

The above example will output:
 
Array 
( 
[color] => green 
[0] => 2 
[1] => 4 
[2] => a 
[3] => b 
[shape] => trapezoid 
[4] => 4 
) 


3. Array_merge_recursive ()

Like the above function, the only difference is that array_merge() is processed by overwriting the previous key values when the key to be added is found to exist at append time, and array_merge_recursive() is processed by reconstructing subarrays to form a new array of numeric values from repeated key values.

Example:
 
<?php 
$ar1 = array("color" => array("favorite" => "red"), 5); 
$ar2 = array(10, "color" => array("favorite" => "green", "blue")); 
$result = array_merge_recursive($ar1, $ar2); 
?> 

The above example will output $result:
 
Array 
( 
[color] => Array 
( 
[favorite] => Array 
( 
[0] => red 
[1] => green 
) 

[0] => blue 
) 

[0] => 5 
[1] => 10 
) 

The split array has two functions:

1. The array_slice ()

Parameter 1 is the target array, parameter 2 is offset and parameter 3 is length. To extract a subarray of length starting from offset from the target array.

If the offset is positive, the starting position will check the offset from the beginning of the array. If the offset is negative, the starting position will check the offset from the end of the array. If the length is positive, there is no doubt that the number of subarray elements retrieved is length. If the length is negative, the subarray starts from offset and ends at the beginning of the array from count(target array) -| length |. Specifically, if the length is empty, the ending position is at the end of the array.

Example:
 
<?php 
$input = array("a", "b", "c", "d", "e"); 

$output = array_slice($input, 2); // returns "c", "d", and "e" 
$output = array_slice($input, -2, 1); // returns "d" 
$output = array_slice($input, 0, 3); // returns "a", "b", and "c" 

// note the differences in the array keys 
print_r(array_slice($input, 2, -1)); 
print_r(array_slice($input, 2, -1, true)); 
?> 

The above example will output:
 
Array 
( 
[0] => c 
[1] => d 
) 
Array 
( 
[2] => c 
[3] => d 
) 

2. The array_splice ()

Take three arguments, same as above, to delete the length of the subarray starting from offset.

Example:
 
<?php 
$input = array("red", "green", "blue", "yellow"); 
array_splice($input, 2); 
// $input is now array("red", "green") 

$input = array("red", "green", "blue", "yellow"); 
array_splice($input, 1, -1); 
// $input is now array("red", "yellow") 

$input = array("red", "green", "blue", "yellow"); 
array_splice($input, 1, count($input), "orange"); 
// $input is now array("red", "orange") 

$input = array("red", "green", "blue", "yellow"); 
array_splice($input, -1, 1, array("black", "maroon")); 
// $input is now array("red", "green", 
// "blue", "black", "maroon") 

$input = array("red", "green", "blue", "yellow"); 
array_splice($input, 3, 0, "purple"); 
// $input is now array("red", "green", 
// "blue", "purple", "yellow"); 
?> 


There are four different value functions:

1. The array_intersect ()

Returns an array of the values of the common elements in all arrays. The keys of the array are given by the keys of the first array.

Example:
 
<?php 
$array1 = array("a" => "green", "red", "blue"); 
$array2 = array("b" => "green", "yellow", "red"); 
$result = array_intersect($array1, $array2); 
?> 

The above example will output:
 
Array 
( 
[a] => green 
[0] => red 
) 


2. The array_intersect_assoc ()

Returns a key-value pair with the same key and value in all arrays, based on the previous function.

Example:
 
<?php 
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); 
$array2 = array("a" => "green", "yellow", "red"); 
$result_array = array_intersect_assoc($array1, $array2); 
?> 

The above example will output:
 
Array 
( 
[a] => green 
) 

3. The array_diff ()

Takes multiple arrays and returns a new array of values that are in the first array but not in the second array, with corresponding keys taken from the first array.

Example:
 
<?php 
$array1 = array("a" => "green", "red", "blue", "red"); 
$array2 = array("b" => "green", "yellow", "red"); 
$result = array_diff($array1, $array2); 

print_r($result); 
?> 

The above example will output:
 
Array 
( 
[1] => blue 
) 


4. The array_diff_assoc ()

On top of the previous function, you need to match not only the value but also the key.

Example:
 
<?php 
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red"); 
$array2 = array ("a" => "green", "yellow", "red"); 
$result = array_diff_assoc($array1, $array2); 
?> 

The above example will output:
 
Array 
( 
[b] => brown 
[c] => blue 
[0] => red 
) 

Related articles: