PHP Array Functions
A function in arrays in PHP is a built-in or user-defined function that helps perform various operations on arrays. These functions allow us to add, remove, search, filter, and modify elements in an array easily. PHP provides many array functions like count(), array_push(), array_merge(), etc., to handle arrays efficiently. Using these functions makes code shorter and easier to manage.
1. array()
This function is used to create an array. It allows you to store multiple values in one variable.
Example:
$fruits = array("Apple", "Banana", "Mango");
print_r($fruits);
2. count()
This function counts the number of elements in an array. It is useful to check how many values are stored.
Example:
$numbers = array(10, 20, 30, 40);
echo count($numbers); // Output: 4
3. array_push()
It adds one or more values at the end of an array.
Example:
$colors = array("Red", "Blue");
array_push($colors, "Green", "Yellow");
print_r($colors);
4. array_pop()
It removes the last element from an array and returns it.
Example:
$items = array("Pen", "Pencil", "Eraser");
array_pop($items);
print_r($items);
5. array_unshift()
This function adds one or more elements to the beginning of an array.
Example:
$numbers = array(2, 3, 4);
array_unshift($numbers, 1);
print_r($numbers);
6. array_shift()
It removes the first element from an array and returns it.
Example:
$cars = array("BMW", "Audi", "Toyota");
array_shift($cars);
print_r($cars);
7. in_array()
This function checks if a value exists in an array. It returns true if found, otherwise false.
Example:
$fruits = array("Apple", "Banana", "Mango");
if (in_array("Banana", $fruits))
{
echo "Found";
}
else
{
echo "Not Found";
}
8. array_merge()
It merges two or more arrays into one new array.
Example:
$arr1 = array(1, 2);
$arr2 = array(3, 4);
$result = array_merge($arr1, $arr2);
print_r($result);
9. array_keys()
This function returns all the keys from an associative array.
Example:
$data = array("name" => "John", "age" => 25);
print_r(array_keys($data));
10. array_values()
It returns all the values from an array without keys.
Example:
$data = array("name" => "John", "age" => 25);
print_r(array_values($data));
11. array_slice()
It extracts a portion of an array without changing the original array.
Example:
$letters = array("a", "b", "c", "d");
print_r(array_slice($letters, 1, 2)); // Output: b, c
12. array_splice()
This function removes or replaces part of an array.
Example:
$letters = array("a", "b", "c", "d");
array_splice($letters, 1, 2, array("x", "y"));
print_r($letters);
13. array_reverse()
It reverses the order of elements in an array.
Example:
$numbers = array(1, 2, 3, 4);
print_r(array_reverse($numbers));
14. array_unique()
This function removes duplicate values from an array.
Example:
$values = array(1, 2, 2, 3, 4, 4);
print_r(array_unique($values));
15. array_sum()
It returns the sum of all numeric values in an array.
Example:
$numbers = array(10, 20, 30);
echo array_sum($numbers); // Output: 60
For more details on functions in array click on the link https://www.php.net/manual/en/ref.array.php