PHP Array Functions
PHP provides several array functions for manipulating and working with arrays.
Example of PHP Array Functions
<?php
$colors = array("red", "green", "blue");
echo count($colors) . "\n";
// Outputs: 3
echo implode(", ", $colors) . "\n";
// Outputs: red, green, blue
$colors[] = "yellow";
echo count($colors) . "\n";
// Outputs: 4
$colors = array_merge($colors, array("orange", "purple"));
echo count($colors) . "\n";
// Outputs: 6
$colors = array_unique($colors);
echo count($colors) . "\n";
// Outputs: 6
$colors = array_reverse($colors);
echo implode(", ", $colors) . "\n";
// Outputs: purple, orange, yellow, blue, green, red
?>
Output:
3
red, green, blue
4
6
6
purple, orange, yellow, blue, green, red
Explanation: This example demonstrates the use of several array functions in PHP, including count()
, implode()
, array_merge()
, array_unique()
, and array_reverse()
.