Array manipulation functions e.g. array_merge(), array_map()
PHP programming offers a variety of array manipulation functions and features built-in, allowing programmers to efficiently modify arrays, merge array elements, and store and process array data elements. These popular array functions help programmers perform common array operations, such as merging two different array elements, filtering array data values, mapping a function to an array, sorting array elements, and more.

So, let’s take a closer look at array manipulation functions in PHP programming.
array_merge() function in PHP.
The array_merge() function in PHP is used to merge two or more individual array elements. Here, if the given array contains string keys, the values in the first array update the values in the second array. If the merged array contains numeric values, they will be added to the array values.
Example of merging arrays in PHP.
<?php
$sample1 = [“macbook pro”, “macbook air”];
$sample2 = [“hp laptop”, “lenovo laptop”];
$output = array_merge($sample1, $sample2);
print_r($output);
// Result – Array ( [0] => macbook pro [1] => macbook air [2] => hp laptop [3] => lenovo laptop )
?>
If the programmer wants to merge arrays containing numeric values into an array.
<?php
$sample1 = [1 => “macbook pro”, 2 => “macbook air”];
$sample2 = [3 => “hp laptop”, 4 => “lenovo laptop”];
$output = array_merge($sample1, $sample2);
print_r($output);
// Result – Array ( [0] => macbook pro [1] => macbook air [2] => hp laptop [3] => lenovo laptop )
?>
array_map() function in PHP.
In PHP programming, the array_map() function applies a callback function to each array element of one or more arrays, and returns a new array value containing the modified element of the array map operation.
Example of the function map on array elements in PHP.
<?php
$integer = [4, 5, 6, 9, 3];
$square_value = array_map(function($integer) {
return $integer * $integer;
}, $integer);
print_r($square_value);
// Result – Array ( [0] => 16 [1] => 25 [2] => 36 [3] => 81 [4] => 9 )
?>
In PHP programming, programmers can also use the array_map() function with multiple arrays.
<?php
$sample1 = [3, 4, 7];
$sample2 = [6, 1, 2];
$output = array_map(function($p, $q) {
return $p + $q;
}, $sample1, $sample2);
print_r($output);
// Result – Array ( [0] => 9 [1] => 5 [2] => 9 )
?>
array_filter() function in PHP.
In PHP programming, the array_filter() function filters the data element values of an array by applying a callback function method. This function returns a new array containing only those array elements for which the callback function returns a true value.
Example of an array filter in PHP.
<?php
$integer = [3, 44, 8, 1, 5, 6, 10];
$eveninteger = array_filter($integer, function($int) {
return $int % 2 == 0;
});
print_r($eveninteger);
// Result – Array ( [1] => 44 [2] => 8 [5] => 6 [6] => 10 )
?>
In this program example, the array filter function displays only even integer values (44, 8, 10) in the filtered array.
array_diff() function in PHP.
In PHP programming, the array_diff() function compares two or more array elements and returns the difference array value that is present in the first array list but not in the other array element.
Example of finding the difference between arrays.
<?php
$sample1 = [9, 7, 2, 8];
$sample2 = [8, 1, 7, 3];
$output = array_diff($sample1, $sample2);
print_r($output);
// Result – Array ( [0] => 9 [2] => 2 )
?>
In the current program, the array_diff() function finds the element values in the array that are in the $sample1 array but not in the $sample2 array.
array_merge_recursive() function in PHP.
In PHP programming, array_merge_recursive() works similarly to the array_merge() function, but this function merges existing array elements recursively. If the existing array contains sub-arrays with the same keys, it merges them into a single array and groups them instead of overwriting the array values.
Example of recursively merging arrays in PHP.
<?php
$sample1 = [“text” => “macbook pro”, “text1” => “macbook air”];
$sample2 = [“text1” => “hp spectre”, “text3” => “dell vostro”];
$output = array_merge_recursive($sample1, $sample2);
print_r($output);
// Result – Array ( [text] => macbook pro [text1] => Array ( [0] => macbook air [1] => hp spectre ) [text3] => dell vostro)
?>
In this program condition, the value of the key “text1” creates an array containing both the “macbook air” and “hp spectre” elements.
array_keys() function in PHP.
In PHP programming, the array_keys() function returns all the keys or array elements of an array. Here, programmers can match specific key values to those keys in an optional order.
Example of getting array keys.
<?php
$list = [“hp” => “hp laptop”, “d” => “dell laptop”, “l” => “lenovo laptop”];
$keys = array_keys($list);
print_r($keys);
// Result – Array ( [0] => hp [1] => d [2] => l )
?>
In PHP programming, you can also pass one of the array_keys() functions to get a specific key value.
<?php
$list = [“hp” => “hp laptop”, “d” => “dell laptop”, “L” => “hp laptop”];
$keys = array_keys($list, “hp laptop”);
print_r($keys);
// Result – Array ( [0] => hp [1] => L )
?>
array_slice() function in PHP.
In PHP programming, the array_slice() function extracts a particular portion of an array. Here, the programmer first indicates the start index of a string and the length of the string portion they want to extract from the existing string.
Example of slicing an array.
<?php
$list = [“macbok pro”, “macbook air”, “hp pavilion”, “dell xps”, “lenovo ideapad”];
$slice = array_slice($list, 2, 5);
print_r($slice);
// Result – Array ( [0] => hp pavilion [1] => dell xps [2] => lenovo ideapad )
?>
This program extracts a portion of an array of length 5, starting at array index 2.
array_push() function in PHP.
In PHP programming, the array_push() function is used to push one or more array elements or data to the end of an existing array.
Example of pushing elements into an array in PHP.
<?php
$profession = [“Engineer”, “Doctor”, “Builder”];
array_push($profession, “Accountant”, “Architect”);
print_r($profession);
// Result – Array ( [0] => Engineer [1] => Doctor [2] => Builder [3] => Accountant [4] => Architect )
?>
array_pop() function in PHP.
In PHP programming, the array_pop() function displays an array element by removing the end data element from an existing array.
Example of popping an element from an array.
<?php
$profession = [“Engineer”, “Doctor” , “Builder”];
$end_element = array_pop($profession);
echo $end_element; // Result – Builder
print_r($profession);
// Result – BuilderArray ( [0] => Engineer [1] => Doctor )
?>
array_sum() function in PHP.
In PHP programming, the array_sum() function displays the total of all the values in an existing array.
Example of array value sum.
<?php
$grouparray = [9, 7, 5, 2, 8, 1];
$total = array_sum($grouparray);
echo $total; // Result – 32
?>
array_reverse() function in PHP.
In PHP programming, the array_reverse() function reverses the order of element data in an existing array.
Example of reversing an array in PHP.
<?php
$list = [“php”, “java”, “python”, “javascript”];
$reversed = array_reverse($list);
print_r($reversed);
// Result – Array ( [0] => javascript [1] => python [2] => java [3] => php )
?>
Explanation of Array Manipulation Functions.
Array Function | Array function Description |
array_merge() function | The array_merge() function used to Merges two or more arrays in list. |
array_map() function | The array_map() function used to Applies a callback function to each element in an active array. |
array_filter() function | The array_filter() function used to Filters array elements use a callback function in the array. |
array_diff() function | The array_diff() function used to Returns elements from the first array that are not in the others array list. |
array_merge_recursive() function | The array_merge_recursive() function Merges arrays recursively (combines sub-arrays) in list of array. |
array_keys() function | The array_keys() function used to Returns the keys of the array. |
array_slice() function | The array_slice() function to Extracts a portion of the particular array. |
array_push() function | The array_push() function used to Adds one or more elements to the end of the array. |
array_pop() function | The array_pop() function used to Removes the last element from the active array. |
array_sum() function | The array_sum() function used to Returns the sum of the values in the array. |
array_reverse() function | The array_reverse() function used to Reverses the order of elements in the active array. |
These functions in PHP programming allow PHP programmers to manipulate array element data types. Whether PHP programmers merge arrays, filter array element values, or convert or transform array data types, these array functions help PHP programmers efficiently manage and control arrays.