Array Short Functions
PHP provides several array short functions that allow for concise and efficient manipulation of arrays. These include commonly used functions for filtering, mapping, reducing, and other operations. Let's explore some of the most important ones:
1. array_map()
Purpose: Applies a callback function to each element of one or more arrays, returning a new array with the results.
Syntax:
php
Copy code
array array_map(callable $callback, array $array, ...$arrays)
Example:
php
Copy code
$numbers = [1, 2, 3, 4];
$squared = array_map(fn($n) => $n * $n, $numbers);
print_r($squared);
Output:
php
Copy code
Array
(
[0] => 1
[1] => 4
[2] => 9
[3] => 16
)
2. array_filter()
Purpose: Filters elements of an array using a callback function. It returns an array containing only the elements for which the callback returns true.
Syntax:
php
Copy code
array array_filter(array $array, callable $callback = null, int $mode = 0)
Example:
php
Copy code
$numbers = [1, 2, 3, 4, 5];
$even = array_filter($numbers, fn($n) => $n % 2 == 0);
print_r($even);
Output:
php
Copy code
Array
(
[1] => 2
[3] => 4
)
3. array_reduce()
Purpose: Iteratively reduces an array to a single value using a callback function.
Syntax:
php
Copy code
mixed array_reduce(array $array, callable $callback, mixed $initial = null)
Example:
php
Copy code
$numbers = [1, 2, 3, 4];
$sum = array_reduce($numbers, fn($carry, $n) => $carry + $n, 0);
echo $sum;
Output:
php
Copy code
10
4. array_walk()
Purpose: Applies a callback function to each element of an array, modifying the array in-place. It doesn't return a new array but modifies the original.
Syntax:
php
Copy code
bool array_walk(array &$array, callable $callback, mixed $userdata = null)
Example:
php
Copy code
$numbers = [1, 2, 3];
array_walk($numbers, fn(&$n) => $n *= 2);
print_r($numbers);
Output:
php
Copy code
Array
(
[0] => 2
[1] => 4
[2] => 6
)
5. array_merge()
Purpose: Merges one or more arrays into a single array.
Syntax:
php
Copy code
array array_merge(array ...$arrays)
Example:
php
Copy code
$array1 = [1, 2];
$array2 = [3, 4];
$merged = array_merge($array1, $array2);
print_r($merged);
Output:
php
Copy code
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
6. array_key_exists()
Purpose: Checks if a specified key or index exists in an array.
Syntax:
php
Copy code
bool array_key_exists(mixed $key, array $array)
Example:
php
Copy code
$array = ['name' => 'John', 'age' => 25];
if (array_key_exists('age', $array)) {
echo "Key exists!";
}
Output:
php
Copy code
Key exists!
7. array_keys() and array_values()
Purpose:
array_keys() returns all the keys from an array.
array_values() returns all the values from an array.
Syntax:
php
Copy code
array array_keys(array $array)
array array_values(array $array)
Example:
php
Copy code
$array = ['name' => 'John', 'age' => 25];
print_r(array_keys($array)); // Outputs keys
print_r(array_values($array)); // Outputs values
Output:
php
Copy code
Array
(
[0] => name
[1] => age
)
Array
(
[0] => John
[1] => 25
)
8. array_slice()
Purpose: Extracts a portion of an array and returns it as a new array.
Syntax:
php
Copy code
array array_slice(array $array, int $offset, int $length = null, bool $preserve_keys = false)
Example:
php
Copy code
$array = [1, 2, 3, 4, 5];
$slice = array_slice($array, 2);
print_r($slice);
Output:
php
Copy code
Array
(
[0] => 3
[1] => 4
[2] => 5
)
9. array_unique()
Purpose: Removes duplicate values from an array.
Syntax:
php
Copy code
array array_unique(array $array, int $flags = SORT_STRING)
Example:
php
Copy code
$array = [1, 2, 2, 3, 3, 4];
$unique = array_unique($array);
print_r($unique);
Output:
php
Copy code
Array
(
[0] => 1
[1] => 2
[3] => 3
[5] => 4
)
These array functions are very useful for manipulating and processing data efficiently in PHP.