Course Content
About Lesson

Commonly Used PHP Array Functions

Returns the count of elements in an array.

PHP
<?php
$fruits = array("Apple", "Banana", "Orange");
echo "Total fruits: " . count($fruits);
?>

Output: Total fruits: 3

Adds one or more items to the end of an array.

PHP
<?php
$fruits = array("Apple", "Banana");
array_push($fruits, "Orange", "Mango");

print_r($fruits);
?>

Output: Array ( [0] => Apple [1] => Banana [2] => Orange [3] => Mango )

Combines two or more arrays into one.

PHP
<?php
$arr1 = array("red", "blue");
$arr2 = array("green", "yellow");

$merged = array_merge($arr1, $arr2);
print_r($merged);
?>

Output: Array ( [0] => red [1] => blue [2] => green [3] => yellow )

Splits a string into an array by using a delimiter.

PHP
<?php
$text = "Apple,Banana,Orange";
$fruits = explode(",", $text);

print_r($fruits);
?>

Output: Array ( [0] => Apple [1] => Banana [2] => Orange )

Opposite of explode(), this function combines array elements into a string.

PHP
<?php
$fruits = array("Apple", "Banana", "Orange");
$text = implode(" - ", $fruits);

echo $text;
?>

Checks if a value exists in an array.

PHP
<?php
$fruits = array("Apple", "Banana", "Orange");

if (in_array("Banana", $fruits)) {
    echo "Banana is available!";
}
?>

Output: Banana is available!

Get all keys or values from an array.

PHP
<?php
$student = array("name" => "John", "age" => 20, "grade" => "A");

print_r(array_keys($student));   // Output: Array ( [0] => name [1] => age [2] => grade )
print_r(array_values($student)); // Output: Array ( [0] => John [1] => 20 [2] => A )
?>

Output: Banana is available!