PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

Built-in array functions in PHP are a set of powerful tools that allow developers to manipulate and manage arrays efficiently. Here’s an in-depth look at some of the most commonly used array functions in PHP:

1. array()

Creates an array.

				
					$arr = array(1, 2, 3, 4);

				
			

2. array_change_key_case()

Changes all keys in an array to lowercase or uppercase.

				
					$array = array("A" => 1, "B" => 2);
$array = array_change_key_case($array, CASE_LOWER);

				
			

3. array_chunk()

Splits an array into chunks of new arrays.

				
					$array = array(1, 2, 3, 4, 5);
$chunks = array_chunk($array, 2);

				
			

4. array_column()

Returns the values from a single column in the input array.

				
					$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
);
$first_names = array_column($records, 'first_name');

				
			

5. array_combine()

Creates an array by using one array for keys and another for its values.

				
					$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

				
			

6. array_count_values()

Counts all the values of an array.

				
					$array = array(1, "hello", 1, "world", "hello");
$counted_values = array_count_values($array);

				
			

7. array_diff()

Computes the difference of arrays.

				
					$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

				
			

8. array_filter()

Filters elements of an array using a callback function.

				
					$input = array("a" => 1, "b" => 2, "c" => 3, "d" => 4);
$result = array_filter($input, function($value) {
    return $value > 2;
});

				
			

9. array_flip()

Exchanges all keys with their associated values in an array.

				
					$array = array("a" => 1, "b" => 2, "c" => 3);
$flipped = array_flip($array);

				
			

10. array_key_exists()

Checks if the given key or index exists in the array.

				
					$array = array("first" => 1, "second" => 4);
if (array_key_exists("first", $array)) {
    echo "Key exists!";
}

				
			

11. array_keys()

Returns all the keys or a subset of the keys of an array.

				
					$array = array(0 => 100, "color" => "red");
$keys = array_keys($array);

				
			

12. array_map()

Applies the callback to the elements of the given arrays.

				
					$func = function($value) {
    return $value * $value;
};
$array = array(1, 2, 3, 4, 5);
$result = array_map($func, $array);

				
			

13. array_merge()

Merges one or more arrays.

				
					$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);

				
			

14. array_pop()

Pops the element off the end of the array.

				
					$array = array("red", "green", "blue");
$popped_value = array_pop($array);

				
			

15. array_push()

Pushes one or more elements onto the end of the array.

				
					$array = array("red", "green");
array_push($array, "blue", "yellow");

				
			

16. array_reverse()

Returns an array with elements in reverse order.

				
					$input = array("php", 4.0, array("green", "red"));
$reversed = array_reverse($input);

				
			

17. array_search()

Searches the array for a given value and returns the corresponding key if successful.

				
					$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array);

				
			

18. array_shift()

Shifts an element off the beginning of the array.

				
					$array = array("red", "green", "blue");
$shifted_value = array_shift($array);

				
			

19. array_slice()

Extracts a slice of the array.

				
					$input = array("a", "b", "c", "d", "e");
$slice = array_slice($input, 2);

				
			

20. array_splice()

Removes a portion of the array and replaces it with something else.

				
					$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);

				
			

21. array_sum()

Returns the sum of values in an array.

				
					$array = array(2, 4, 6, 8);
$sum = array_sum($array);

				
			

22. array_unique()

Removes duplicate values from an array.

				
					$array = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($array);

				
			

23. array_unshift()

Prepends one or more elements to the beginning of an array.

				
					$array = array("blue", "green");
array_unshift($array, "red", "yellow");

				
			

24. array_values()

Returns all the values of an array.

				
					$array = array("size" => "XL", "color" => "gold");
$values = array_values($array);

				
			

25. array_walk()

Applies a user function to every member of an array.

				
					$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

function test_alter(&$item1, $key, $prefix)
{
    $item1 = "$prefix: $item1";
}

array_walk($fruits, 'test_alter', 'fruit');

				
			

These functions represent only a portion of PHP’s extensive array manipulation capabilities. By understanding and utilizing these functions, developers can effectively manage and transform arrays, making their code more efficient and readable.

Scroll to Top