Most common PHP array functions.
PHP provides a wide range of array functions to manipulate and work with arrays. Here are some of the most commonly used array functions in PHP:
PHP provides a wide range of array functions to manipulate and work with arrays. Here are some of the most commonly used array functions in PHP:
1. count: Returns the number of elements in an array.
$count = count($array);
2. array_merge: Combines multiple arrays into a single array,not for multidimensional array.
$result = array_merge($array1, $array2, ...);
$array1 = array('a', 'b', 'c');
$array2 = array(1 => 'd');
$resultArray = array_merge($array1, $array2);
print_r($resultArray);
Array ( [0] => a [1] => b [2] => c [3] => d )
array_push($array, $element1, $element2, ...);
$multidimensionalArray = array(
'colors' => array('red', 'green'),
'fruits' => array('apple')
);
// Using array_push to add a new color to the 'colors' sub-array
array_push($multidimensionalArray['colors'], 'blue');
// Using array_push to add a new fruit to the 'fruits' sub-array
array_push($multidimensionalArray['fruits'], 'orange');
print_r($multidimensionalArray);
Array ( [colors] => Array ( [0] => red [1] => green [2] => blue ) [fruits] => Array ( [0] => apple [1] => orange ) )
$lastElement = array_pop($array);
5. array_shift: Removes and returns the first element from an array.
$firstElement = array_shift($array);
$array = array('apple', 'banana');
$firstElement = array_shift($array);
echo "Removed Element: " . $firstElement . "\n";
print_r($array);
Removed Element: apple
Array ( [0] => banana )
array_unshift($array, $element1, $element2, ...);
7. array_slice: Extracts a slice of an array.
$subset = array_slice($array, $start, $length);
8. array_splice: Removes a portion of the array and returns it.
$removed = array_splice($array, $start, $length);
$ms = array(
'colors' => array('red', 'greenRemove', 'blue'),
);
// Remove 'green' from the 'colors' sub-array
array_splice($ms['colors'], 1, 1);
print_r($ms);
echo "<br />";
// Add 'purple' at index 1 in the 'colors' sub-array
array_splice($ms['colors'], 1, 1 , 'purple');
print_r($ms);
echo "<br />";
// Add 'purple' at index 1 in the 'colors' sub-array
array_splice($ms['colors'], 1 , 0 , 'ORANGE');
print_r($ms);
Array ( [colors] => Array ( [0] => red [1] => blue ) )
Array ( [colors] => Array ( [0] => red [1] => purple ) )
Array ( [colors] => Array ( [0] => red [1] => ORANGE [2] => purple ) )
if (array_key_exists($key, $array)) {
// Key exists
}
10. in_array: Checks if a value exists in an array.
if (in_array($value, $array)) {
// Value exists
}
11. array_search: Searches for a value in an array and returns its key.
$key = array_search($value, $array);
12. array_filter: Filters elements of an array using a callback function.
$filteredArray = array_filter($array, function($element) {
return /* some condition */;
});
13. array_map: Applies a callback function to each element of an array.
$resultArray = array_map(function($element) {
return /* some transformation */;
}, $array);
14. array_reduce: Reduces an array to a single value using a callback function.
$result = array_reduce($array, function($carry, $item) {
return /* some reduction logic */;
});
These are just a few examples of the many array functions available in PHP. The PHP documentation is an excellent resource for exploring all available array functions and their use cases: PHP Array Functions.
Comments
Post a Comment