php operation array (merge split append find delete etc.)

  • 2020-05-19 04:21:49
  • OfStack

1. Merge the array
The array_merge() function merges the array into 1 and returns an associated array. The resulting array begins with the first input array parameter, and is forced in the order in which the array parameters appear. Its form is:
 
array array_merge (array array1 array2 ... ,arrayN) 

This function merges the elements of one or more arrays, with the values of one array appended to the first array. Returns an array as a result.
If the input array has the same string key name, the value after that key name overrides the first value. However, if the array contains a numeric key name, the subsequent value will not override the original value, but will be appended to it.
If only one array is given and the array is numerically indexed, the key name is continuously re-indexed.
 
<?php 
$fruits = array("apple","banana","pear"); 
$numbered = array("1","2","3"); 
$cards = array_merge($fruits, $numbered); 
print_r($cards); 
// output 
// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 ) 
?> 

2. Append an array
The array_merge_recursive() function is the same as array_merge() in that you can combine two or more arrays in 1 to form a combined array. The difference between the two is that the function takes a different approach when a key in an input array already exists in the result array. array_merge() overwrites the previous key/value pair and replaces it with the key/value pair in the current input array, while array_merge_recursive() will combine the two values in 1 to form a new array with the original key as the array name. There's also the form of a combination of one number, which is a recursively appended array. Its form is:

 
array array_merge_recursive(array array1,array array2[ ... ,array arrayN]) 

The program example is as follows:
 
<?php 
$fruit1 = array("apple" => "red", "banana" => "yellow"); 
$fruit2 = array("pear" => "yellow", "apple" => "green"); 
$result = array_merge_recursive($fruit1, $fruit2); 
print_r($result); 
// output 
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) 
?> 

The key apple now points to an array that is indexed by two color values.
3. Concatenate arrays
The array_combine() function gets a new array of submitted keys and corresponding values. Its form is:
 
array array_combine(array keys,array values) 

Note that the two input arrays must be the same size and cannot be empty. The sample is as follows
 
<?php 
$name = array("apple", "banana", "orange"); 
$color = array("red", "yellow", "orange"); 
$fruit = array_combine($name, $color); 
print_r($fruit); 
// output 
// Array ( [apple] => red [banana] => yellow [orange] => orange ) 
?> 

4. Split array array_slice()
The array_slice() function returns part 1 of the array, starting with the offset key and ending at offset+length. In the form:

 
array array_slice (array array, int offset[,int length]) 

When offset is positive, the split starts at the offset position at the beginning of the array. If offset is negative, the split starts at the offset position at the end of the array. If the optional parameter length is omitted, the split will start at offset, 1 up to the last element of the array. If length is given and is positive, it ends at offset+length from the beginning of the array. Conversely, if length is given and is negative, it ends at count(input_array)-|length| from the beginning of the array. Consider an example:
 
<?php 
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon"); 
$subset = array_slice($fruits, 3); 
print_r($subset); 
// output 
// Array ( [0] => Pear [1] => Grape [2] => Lemon [3] => Watermelon ) 
?> 

Then we use the following negative length:
 
<?php 
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon"); 
$subset = array_slice($fruits, 2, -2); 
print_r($subset); 
// output 
// Array ( [0] => Orange [1] => Pear [2] => Grape ) 
?> 

5. Join array array_splice()
The array_splice() function deletes all elements in the array from the beginning of offset to the end of offset+length, and returns the deleted elements as an array. Its form is:
 
array array_splice ( array array , int offset[,length[,array replacement]]) 

When offset is positive, engagement will start at offset at the beginning of the array, and when offset is negative, engagement will start at offset at the end of the array. If you ignore the optional length parameter, all elements from the offset position to the end of the array will be deleted. If length is given and is positive, the join ends at offset + leng th from the beginning of the array. Conversely, if length is given and is negative, the combination ends at count(input_array) -length from the beginning of the array. Examples are as follows:
 
<?php 
$fruits = array("apple","banana","pear"); 
$numbered = array("1","2","3"); 
$cards = array_merge($fruits, $numbered); 
print_r($cards); 
// output 
// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 ) 
?> 
0
You can use the optional parameter replacement to specify the array to replace the target part. Examples are as follows:
 
<?php 
$fruits = array("apple","banana","pear"); 
$numbered = array("1","2","3"); 
$cards = array_merge($fruits, $numbered); 
print_r($cards); 
// output 
// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 ) 
?> 
1
You can see clearly from the program how this function is used.

6. Intersection of arrays array_intersect()
The array_intersect() function returns an array containing the keys, consisting only of the values that appear in the first array and in each of the other input arrays. Its form is as follows:

 
array array_intersect(array array1,array array2[,arrayN ... ]) 

The following example returns all the fruits that appear in the $fruit1 array and also appear in $fruit2 and $fruit3:
 
<?php 
$fruit1 = array("Apple","Banana","Orange"); 
$fruit2 = array("Pear","Apple","Grape"); 
$fruit3 = array("Watermelon","Orange","Apple"); 
$intersection = array_intersect($fruit1, $fruit2, $fruit3); 
print_r($intersection); 
// output 
// Array ( [0] => Apple ) 
?> 

The array_intersect() function considers two elements to be the same only if they are equal and have the same data type.

7. Intersection of associative array array_intersect_assoc()
The function array_intersect_assoc() is basically the same as array_intersect(), except that it also takes into account the array keys in the comparison. Therefore, only key/value pairs that appear in the first array and in all other input arrays are returned to the resulting array.
The form is as follows:

 
<?php 
$fruits = array("apple","banana","pear"); 
$numbered = array("1","2","3"); 
$cards = array_merge($fruits, $numbered); 
print_r($cards); 
// output 
// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 ) 
?> 
4
The following example returns all key/value pairs that appear in the $fruit1 array, as well as in $fruit2 and $fruit3:
 
<?php 
$fruits = array("apple","banana","pear"); 
$numbered = array("1","2","3"); 
$cards = array_merge($fruits, $numbered); 
print_r($cards); 
// output 
// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 ) 
?> 
5
8. Difference set array_diff()
The function array_diff() returns a value that appears in the first array but does not appear in the other input arrays. This function is the opposite of array_intersect().
 
<?php 
$fruits = array("apple","banana","pear"); 
$numbered = array("1","2","3"); 
$cards = array_merge($fruits, $numbered); 
print_r($cards); 
// output 
// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 ) 
?> 
6
Examples are as follows:
 
<?php 
$fruits = array("apple","banana","pear"); 
$numbered = array("1","2","3"); 
$cards = array_merge($fruits, $numbered); 
print_r($cards); 
// output 
// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 ) 
?> 
7
9. Difference set of associative array array_diff_assoc()

The function array_diff_assoc() is basically the same as array_diff(), except that it also takes into account the keys of the array when comparing. Therefore, only key/value pairs that appear in the first array and not in other input arrays are returned to the resulting array. Its form is as follows:
 
<?php 
$fruits = array("apple","banana","pear"); 
$numbered = array("1","2","3"); 
$cards = array_merge($fruits, $numbered); 
print_r($cards); 
// output 
// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 ) 
?> 
8
The following example only returns [yellow] = > Banana, because this particular key/value pair appears in $fruit1, but not in $fruit2 or $fruit3.
 
<?php 
$fruits = array("apple","banana","pear"); 
$numbered = array("1","2","3"); 
$cards = array_merge($fruits, $numbered); 
print_r($cards); 
// output 
// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 ) 
?> 
9
Using arrays often involves traversing groups of Numbers. You usually need to go through groups and get individual keys or values (or both), so it's no surprise that PHP provides some functions for this purpose. Many functions perform two tasks, not only getting the key or value for the current pointer position, but also moving the pointer down one appropriate position.

10. Get the current array key key()
The key() function returns the key at which the current pointer is located in input_array. Its form is as follows:
 
array array_merge_recursive(array array1,array array2[ ... ,array arrayN]) 
0
The following example iterates through the array and moves the pointer to output the key of the $fruits array:
 
$fruits = array("apple"=>"red", "banana"=>"yellow"); 
while ($key = key($fruits)) { 
printf("%s <br />", $key); 
next($fruits); 
} 
// apple 
// banana 

Note that the pointer is not moved each time key() is called. To do this, use the next() function, whose only function is to push the pointer.

11. Get the current array value current()
The current() function returns the array value of the current position of the pointer in the array. Its form is as follows:
 
mixed current(array array) 

Modify the previous example to get the array value this time:
 
$fruits = array("apple"=>"red", "banana"=>"yellow"); 
while ($fruit = current($fruits)) { 
printf("%s <br />", $fruit); 
next($fruits); 
} 
// red 
// yellow 

12. Get the current array key and value each()
The each() function returns the current key/value pair of input_array and pushes the pointer one position. Its form is as follows:
 
array each(array array) 

The returned array contains four keys, keys 0 and key contain the key names, and keys 1 and value contain the corresponding data. If the pointer before executing each() is at the end of the array, false is returned.
 
$fruits = array("apple", "banana", "orange", "pear"); 
print_r ( each($fruits) ); 
// Array ( [1] => apple [value] => apple [0] => 0 [key] => 0 ) 

each() is often used in conjunction with list() to traverse groups. This example is similar to the previous one, but loops out the entire array:
 
$fruits = array("apple", "banana", "orange", "pear"); 
reset($fruits); 
while (list($key, $val) = each($fruits)) 
{ 
echo "$key => $val<br />"; 
} 
// 0 => apple 
// 1 => banana 
// 2 => orange 
// 3 => pear 

Because assigning one array to another array resets the pointer to the original array, in the example above if we assign $fruits to another variable inside the loop it will cause an infinite loop.
This completes the traversal of the array.

Finding, filtering, and searching array elements are common features of array operations. Let's introduce a couple of related functions.

13. in_array () function
The in_array() function searches an array for a specific value. If it finds the value, it returns true; otherwise, it returns false. Its form is as follows:
 
array array_merge_recursive(array array1,array array2[ ... ,array arrayN]) 
7
Look at the following example to see if the variable apple is already in the array, and if so, output 1 segment of information:
 
array array_merge_recursive(array array1,array array2[ ... ,array arrayN]) 
8
The third parameter, optional, forces in_array() to consider the type when searching.


14. array_key_exists () function
If a specified key is found in an array, the function array_key_exists() returns true, otherwise false. Its form is as follows:
 
boolean array_key_exists(mixed key,array array); 

The following example will search for apple in the array key and, if found, output the color of the fruit:
 
<?php 
$fruit1 = array("apple" => "red", "banana" => "yellow"); 
$fruit2 = array("pear" => "yellow", "apple" => "green"); 
$result = array_merge_recursive($fruit1, $fruit2); 
print_r($result); 
// output 
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) 
?> 
0
15. array_search () function
The array_search() function searches an array for a specified value and returns the corresponding key if found, otherwise returns false. Its form is as follows:
 
<?php 
$fruit1 = array("apple" => "red", "banana" => "yellow"); 
$fruit2 = array("pear" => "yellow", "apple" => "green"); 
$result = array_merge_recursive($fruit1, $fruit2); 
print_r($result); 
// output 
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) 
?> 
1
The following example searches $fruits for a specific date (December 7) and, if found, returns information about the appropriate state:
 
<?php 
$fruit1 = array("apple" => "red", "banana" => "yellow"); 
$fruit2 = array("pear" => "yellow", "apple" => "green"); 
$result = array_merge_recursive($fruit1, $fruit2); 
print_r($result); 
// output 
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) 
?> 
2
16. array_keys () function
The array_keys() function returns an array containing all the keys found in the searched array. Its form is as follows:

 
<?php 
$fruit1 = array("apple" => "red", "banana" => "yellow"); 
$fruit2 = array("pear" => "yellow", "apple" => "green"); 
$result = array_merge_recursive($fruit1, $fruit2); 
print_r($result); 
// output 
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) 
?> 
3
If you include the optional parameter search_value, only the keys matching that value will be returned. The following example will output all arrays found in the $fruit array:
 
<?php 
$fruit1 = array("apple" => "red", "banana" => "yellow"); 
$fruit2 = array("pear" => "yellow", "apple" => "green"); 
$result = array_merge_recursive($fruit1, $fruit2); 
print_r($result); 
// output 
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) 
?> 
4
17. array_values () function
The array_values() function returns all the values in an array and automatically provides a numeric index to the returned array. Its form is as follows:
 
<?php 
$fruit1 = array("apple" => "red", "banana" => "yellow"); 
$fruit2 = array("pear" => "yellow", "apple" => "green"); 
$result = array_merge_recursive($fruit1, $fruit2); 
print_r($result); 
// output 
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) 
?> 
5
The following example will get the values of each element found in $fruits:
 
<?php 
$fruit1 = array("apple" => "red", "banana" => "yellow"); 
$fruit2 = array("pear" => "yellow", "apple" => "green"); 
$result = array_merge_recursive($fruit1, $fruit2); 
print_r($result); 
// output 
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) 
?> 
6
Sometimes we need to extend an array, or delete the first part of an array. PHP provides some functions for extending and shrinking arrays. These functions are handy for programmers who want to emulate various queue implementations (FIFO, LIFO). As the name implies, the names of these functions (push, pop, shift, and unshift) clearly reflect what they do.
PS: a traditional queue is a data structure in which elements are removed in the same order as they are added, known as fifo, or FIFO. Instead, the stack is another data structure in which elements are removed in the opposite order as they were added, which is called lifo, or LIFO.


18. Add an element to the array header
The array_unshift() function adds an element to the array header. All existing numeric keys are modified to reflect their new position in the array, but the associated keys are not affected. Its form is as follows:
 
<?php 
$fruit1 = array("apple" => "red", "banana" => "yellow"); 
$fruit2 = array("pear" => "yellow", "apple" => "green"); 
$result = array_merge_recursive($fruit1, $fruit2); 
print_r($result); 
// output 
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) 
?> 
7
The following example adds two fruits to the $fruits array:
 
<?php 
$fruit1 = array("apple" => "red", "banana" => "yellow"); 
$fruit2 = array("pear" => "yellow", "apple" => "green"); 
$result = array_merge_recursive($fruit1, $fruit2); 
print_r($result); 
// output 
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) 
?> 
8
19. Add an element to the end of the array
The return value of the array_push() function is of type int, which is the number of elements in the array after the data is pressed. You can pass multiple variables as arguments to this function and press multiple variables into the array at the same time. Its form is:
 
<?php 
$fruit1 = array("apple" => "red", "banana" => "yellow"); 
$fruit2 = array("pear" => "yellow", "apple" => "green"); 
$result = array_merge_recursive($fruit1, $fruit2); 
print_r($result); 
// output 
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) 
?> 
9
The following example adds two more fruits to the $fruits array:
 
$fruits = array("apple","banana"); 
array_push($fruits,"orange","pear") 
//$fruits = array("apple","banana","orange","pear") 

20. Deletes a value from the array header
The array_shift() function deletes and returns the elements found in the array. As a result, if a numeric key is used, all the corresponding values are moved down, leaving the array using the associated key unaffected. In the form of
 
mixed array_shift(array array) 

The following example removes the first element of the $fruits array, apple:
 
array array_combine(array keys,array values) 
2
21. Delete elements from the end of the array
The array_pop() function deletes and returns the last element of the array. Its form is:
 
mixed array_pop(aray target_array); 

The following example removes the last state from the $states array:
 
$fruits = array("apple","banana","orange","pear"); 
$fruit = array_pop($fruits); 
//$fruits = array("apple","banana","orange"); 
//$fruit = "pear"; 

Related articles: