Arrays
Array
An array in PHP is a variable that holds more than one value at a time, and you can access each value using its position or a name.
Array Types
In PHP, arrays are mainly of three types. Arrays are used to store multiple values in a single variable in different ways.
1. Indexed Array
An indexed array stores values with numeric indexes (0, 1, 2, …).
2. Associative Array
An associative array stores values using named keys (like names instead of numbers).
3. Multidimensional Array
A multidimensional array is an array that contains another array inside it.
Indexed Array
An indexed array in PHP is a type of array where each element is stored in a specific position called an index number.
The index always starts from 0, so:
- First element → index 0
- Second element → index 1
- Third element → index 2, and so on
This means every value in the array is accessed using its number position.
Example
// Creating an indexed array of cars
$cars = array("Volvo", "BMW", "Toyota");
// Changing the value at index 1 (BMW is replaced with Ford)
$cars[1] = "Ford";
// var_dump() is used to display full details of the array
var_dump($cars);
?>
Associative Array
An associative array is an array that stores data in key-value pairs, where each value is accessed by a custom name (key) instead of a numeric index.
Example
$person = [
"name" => "Rahul",
"age" => 25,
"city" => "Bhopal"
];
echo $person["name"];
echo $person["age"];
echo $person["city"];
?>
Create Array
You can create arrays in different ways depending on how you want to store data.
1. Using array() Function
2. Using Short Syntax (Preferred Method)
3. Creating Associative Array
4. Creating Empty Array
5. Mixing Array Keys
Example
// 1. Using array() Function
$array1 = array("Apple", "Banana", "Mango");
// Old traditional way of creating an indexed array
// 2. Using Short Syntax (Preferred Method)
$array2 = ["Red", "Green", "Blue"];
// Modern and recommended way (short syntax)
// 3. Creating Associative Array
$array3 = [
"name" => "John",
"age" => 25,
"city" => "Delhi"
];
// Key => Value pairs (useful for structured data)
// 4. Creating Empty Array
$array4 = [];
// Empty array, can add elements later
// Adding values to empty array
$array4[] = "First Value";
$array4[] = "Second Value";
// 5. Mixing Array Keys
$array5 = [
0 => "Zero Index",
"fruit" => "Apple",
1 => "One Index",
"color" => "Red"
];
// Mix of numeric and associative keys
// Output all arrays using print_r for readability
echo "Array 1:\n";
print_r($array1);
echo "\nArray 2:\n";
print_r($array2);
echo "\nArray 3 (Associative):\n";
print_r($array3);
echo "\nArray 4 (Empty then filled):\n";
print_r($array4);
echo "\nArray 5 (Mixed Keys):\n";
print_r($array5);
?>
Sorting Arrays
In PHP, sorting arrays is easy because there are built-in functions that do the work for you.
But different functions are used depending on the type of array:
- Indexed Array → normal list (0, 1, 2…)
- Associative Array → key-value pairs (name => "John")
Array Sorting Functions
- sort() - sorts an indexed array in ascending order
- rsort() - sorts an indexed array in descending order
- asort() - sorts an associative array in ascending order, according to the value
- ksort() - sorts an associative array in ascending order, according to the key
- arsort() - sorts an associative array in descending order, according to the value
- krsort() - sorts an associative array in descending order, according to the key
Example
// Indexed Array Example
$numbers = [5, 2, 9, 1];
// sort() → Ascending order (small to big)
sort($numbers);
echo "\nsort() - Ascending Order:\n";
print_r($numbers);
// rsort() → Descending order (big to small)
rsort($numbers);
echo "\nrsort() - Descending Order:\n";
print_r($numbers);
// Associative Array Example
$data = [
"b" => "Banana",
"a" => "Apple",
"c" => "Cherry"
];
echo "\nOriginal Associative Array:\n";
print_r($data);
// asort() → Sort by values (A to Z), keep keys
asort($data);
echo "\nasort() - Sort by VALUE (Ascending):\n";
print_r($data);
// arsort() → Sort by values (Z to A), keep keys
arsort($data);
echo "\narsort() - Sort by VALUE (Descending):\n";
print_r($data);
// ksort() → Sort by keys (A to Z)
ksort($data);
echo "\nksort() - Sort by KEY (Ascending):\n";
print_r($data);
// krsort() → Sort by keys (Z to A)
krsort($data);
echo "\nkrsort() - Sort by KEY (Descending):\n";
print_r($data);
?>
Remove Array Items
Removing array items in PHP means deleting one or more elements from an array using built-in functions.
Removing / Deleting Array Items in PHP Using Different Functions
- array_splice() - removes a portion of the array starting from a start position and length
- unset() - removes the element associated with a specific key
- array_diff() - remove items from an associative array
- array_pop() - removes the last array item
- array_shift() - removes the first array item
Example
// ======================
// Original Array
// ======================
$fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"];
echo "Original Array:\n";
print_r($fruits);
// =====================================
// 1. array_splice()
// Removes part of array using start position & length
// =====================================
$temp1 = $fruits; // Copy original array
array_splice($temp1, 1, 2);
// Removes 2 elements starting from index 1 ("Banana", "Mango")
echo "\nAfter array_splice() (remove from index 1, length 2):\n";
print_r($temp1);
// =====================================
// 2. unset()
// Removes element using specific key (index)
// =====================================
$temp2 = $fruits;
unset($temp2[2]);
// Removes element at index 2 ("Mango")
echo "\nAfter unset() (remove index 2):\n";
print_r($temp2);
// =====================================
// 3. array_diff()
// Removes specific values from array
// =====================================
$temp3 = $fruits;
$result = array_diff($temp3, ["Banana", "Grapes"]);
// Removes "Banana" and "Grapes"
echo "\nAfter array_diff() (remove Banana & Grapes):\n";
print_r($result);
// =====================================
// 4. array_pop()
// Removes last element of array
// =====================================
$temp4 = $fruits;
array_pop($temp4);
// Removes last element ("Grapes")
echo "\nAfter array_pop() (remove last item):\n";
print_r($temp4);
// =====================================
// 5. array_shift()
// Removes first element of array
// =====================================
$temp5 = $fruits;
array_shift($temp5);
// Removes first element ("Apple")
echo "\nAfter array_shift() (remove first item):\n";
print_r($temp5);
?>
Array Functions
| Function | Use |
|---|---|
| array() | Creates an array |
| range() | Creates an array with a range of values |
| count() | Counts number of elements in array |
| array_push() | Adds elements at the end of array |
| array_pop() | Removes last element |
| array_shift() | Removes first element |
| array_unshift() | Adds elements at the beginning |
| unset() | Removes specific element by key/index |
| array_splice() | Removes or replaces part of array |
| in_array() | Checks if a value exists in array |
| array_search() | Finds key of a value |
| array_keys() | Returns all keys |
| array_values() | Returns all values |
| array_merge() | Combines multiple arrays |
| array_unique() | Removes duplicate values |
| array_reverse() | Reverses array order |
| sort() | Sorts array in ascending order |
| rsort() | Sorts array in descending order |
| asort() | Sorts associative array by value (ASC) |
| arsort() | Sorts associative array by value (DESC) |
| ksort() | Sorts associative array by key (ASC) |
| krsort() | Sorts associative array by key (DESC) |
| array_sum() | Returns sum of values |
| array_product() | Returns product of values |
| array_filter() | Filters array elements |
| array_map() | Applies function to each element |