Array stores multiple values in one variable:

Using the array() function for creating array

 2nd method to create array Using square bracket shorthand [] (available since PHP 5.4):

Creating an Array:

1. Using the array() function.

$fruits = array("apple", "orange", "banana");

2. Using square bracket shorthand (available since PHP 5.4):

$fruits = ["apple", "orange", "banana"];

Array Types:

  1. Indexed Arrays
  2. Associative Arrays
  3. Multidimensional arrays

1. Indexed Arrays:

An indexed array uses numeric index to access elements. The index starts from 0.

<?php
$fruits = array("apple", "orange", "banana");
echo $fruits[1]; // Outputs: orange
?>

2. Associative Arrays:

Associative array use named key to access elements.

<?php
$person = array("name" => "John", "age" => 25, "city" => "New York");
echo $person["name"]; // Outputs: John
?>

3. Multidimensional Arrays:

Array can contain other arrays, Known as  multidimensional array.

<?php
$matrix = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);
echo $matrix[1][2]; // Outputs: 6
?>

Array Functions:

CategoryArray Function NameDefinitionExample
Basic Functionscount()Counts the number of elements in an array.
<?php
$array = ["Apple","Banana","Orange"];
echo count($array); // 3
?>
sizeof()Alias of count().
<?php
$array = ["Apple","Banana","Orange"];
echo sizeof($array); // 3
?>
Adding/Removing Elementsarray_push()Adds one or more elements to the end of an array.
<?php
$array = ["Apple","Banana"];
array_push($array, "Orange");
print_r($array);
?>
array_pop()Removes and returns the last element of an array.
<?php
$array = ["Apple","Banana","Orange"];
$last = array_pop($array);
echo $last; // Orange
?>
array_unshift()Adds one or more elements to the beginning of an array.
<?php
$array = ["Banana","Orange"];
array_unshift($array, "Apple");
print_r($array);
?>
array_shift()Removes and returns the first element of an array.
<?php
$array = ["Apple","Banana","Orange"];
$first = array_shift($array);
echo $first; // Apple
?>
Merging Arraysarray_merge()Merges two or more arrays.
<?php
$array1 = ["Apple","Banana"];
$array2 = ["Orange","Mango"];
$result = array_merge($array1, $array2);
print_r($result);
?>
array_merge_recursive()Recursively merges two or more arrays.
<?php
$array1 = ["color" => ["red"]];
$array2 = ["color" => ["blue"]];
$result = array_merge_recursive($array1, $array2);
print_r($result);
?>
Sortingsort()Sorts an array in ascending order.
<?php
$array = [5,2,8,1];
sort($array);
print_r($array);
?>
rsort()Sorts an array in descending order.
<?php
$array = [5,2,8,1];
rsort($array);
print_r($array);
?>
asort()Sorts an associative array in ascending order while maintaining key-value pairs.
<?php
$array = ["a"=>5,"b"=>2,"c"=>8];
asort($array);
print_r($array);
?>
ksort()Sorts an associative array by keys in ascending order.
<?php
$array = ["c"=>8,"a"=>5,"b"=>2];
ksort($array);
print_r($array);
?>
Searchingin_array()Checks if a value exists in an array.
<?php
$array = ["Apple","Banana","Orange"];

if(in_array("Banana",$array)){
    echo "Value exists";
}
?>
array_search()Searches an array for a value and returns its key.
<?php
$array = ["Apple","Banana","Orange"];
$key = array_search("Banana",$array);
echo $key; // 1
?>
array_key_exists()Checks if a key exists in an array.
<?php
$array = ["name"=>"John","age"=>25];

if(array_key_exists("name",$array)){
    echo "Key exists";
}
?>
Filteringarray_filter()Filters elements of an array using a callback function.
<?php
$array = [10,15,20,25,30];

$result = array_filter($array, function($value){
    return $value > 20;
});

print_r($result);
?>
Othersarray_reverse()Returns an array with elements in reverse order.
<?php
$array = ["Apple","Banana","Orange"];
$result = array_reverse($array);
print_r($result);
?>
array_unique()Removes duplicate values from an array.
<?php
$array = ["Apple","Banana","Apple","Orange"];
$result = array_unique($array);
print_r($result);
?>
array_slice()Returns a slice of an array.
<?php
$array = ["Apple","Banana","Orange","Mango"];
$result = array_slice($array, 1, 2);
print_r($result);
?>