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

Array functions in PHP are a robust set of tools that allow for the manipulation and management of arrays. These functions are crucial for handling various operations such as adding, removing, searching, and sorting elements within an array. Let’s dive deeper into some of the most commonly used array functions in PHP.

1. Creating Arrays

  • array(): This function creates an array.
				
					$array = array(1, 2, 3, 4, 5);

				
			
  • range(): Creates an array containing a range of elements.
				
					$array = array(1, 2, 3, 4, 5);

				
			

2. Adding/Removing Elements

  • array_push(): Adds one or more elements to the end of an array.
				
					array_push($array, 6, 7);

				
			
  • array_pop(): Removes the last element of an array.
				
					$lastElement = array_pop($array);

				
			
  • array_unshift(): Adds one or more elements to the beginning of an array.
				
					array_unshift($array, 0);

				
			
  • array_shift(): Removes the first element of an array.
				
					$firstElement = array_shift($array);

				
			

3. Array Slicing and Splicing

  • array_slice(): Extracts a slice of the array.
				
					$subset = array_slice($array, 2, 3);

				
			
  • array_splice(): Removes a portion of the array and replaces it with something else.
				
					array_splice($array, 2, 2, array('a', 'b'));

				
			

4. Searching Arrays

  • in_array(): Checks if a value exists in an array.
				
					$exists = in_array(3, $array);

				
			

array_search(): Searches the array for a value and returns the corresponding key if found.

				
					$key = array_search(3, $array);

				
			

5. Array Keys and Values

  • array_keys(): Returns all the keys of an array.
				
					$keys = array_keys($array);

				
			

array_values(): Returns all the values of an array.

				
					$values = array_values($array);

				
			

6. Array Merging and Combining

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

				
			

array_combine(): Creates an array by using one array for keys and another for its values.

				
					$keys = array('green', 'red', 'yellow');
$values = array('avocado', 'apple', 'banana');
$combined = array_combine($keys, $values);

				
			

7. Array Sorting

  • sort(): Sorts an array in ascending order.
				
					sort($array);

				
			
  • rsort(): Sorts an array in descending order.
				
					rsort($array);

				
			
  • asort(): Sorts an associative array in ascending order, according to the value.
				
					asort($array);

				
			
  • ksort(): Sorts an associative array in ascending order, according to the key.
				
					ksort($array);

				
			

8. Array Filtering and Mapping

  • array_filter(): Filters elements of an array using a callback function.
				
					$filtered = array_filter($array, function($value) {
  return $value % 2 === 0; // Only even numbers
});

				
			
  • array_map(): Applies a callback to the elements of an array.
				
					$squared = array_map(function($value) {
  return $value * $value;
}, $array);

				
			

9. Array Reduction

  • array_reduce(): Iteratively reduces the array to a single value using a callback function.
				
					$sum = array_reduce($array, function($carry, $item) {
  return $carry + $item;
});

				
			

10. Array Operations

  • array_diff(): Computes the difference of arrays.
				
					$sum = array_reduce($array, function($carry, $item) {
  return $carry + $item;
});

				
			
  • array_intersect(): Computes the intersection of arrays.
				
					$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);

				
			

PHP array functions provide a comprehensive set of tools for manipulating arrays in various ways. Understanding these functions allows for efficient data handling and can significantly simplify complex data manipulation tasks.

Scroll to Top