Arrays in php
An array stores multiple values in a single variable. php supports indexed arrays, associative arrays, and multidimensional arrays.
Creating an Arrayโ
<?php
// Using array()
$fruits = array("Apple", "Banana", "Cherry");
// Using short syntax
$colors = ["red", "green", "blue"];
echo $fruits[0]; // Apple
echo $colors[2]; // blue
?>
Accessing Array Elementsโ
Array indexes start at 0 by default:
<?php
$cars = ["Toyota", "Honda", "BMW"];
echo $cars[0]; // Toyota
echo $cars[1]; // Honda
echo $cars[2]; // BMW
?>
Modifying Array Elementsโ
<?php
$fruits = ["Apple", "Banana", "Cherry"];
$fruits[1] = "Mango"; // replace Banana
$fruits[] = "Grapes"; // append new element
print_r($fruits);
// [Apple, Mango, Cherry, Grapes]
?>
Multidimensional Arraysโ
An array containing one or more arrays:
<?php
$matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
echo $matrix[1][2]; // 6
?>
Nested Associative Arraysโ
<?php
$students = [
["name" => "Alice", "grade" => "A"],
["name" => "Bob", "grade" => "B"],
["name" => "Carol", "grade" => "A+"]
];
foreach ($students as $student) {
echo $student["name"] . ": " . $student["grade"] . "<br>";
}
?>
Useful Array Functionsโ
Countโ
<?php
$arr = [1, 2, 3, 4, 5];
echo count($arr); // 5
?>
Sortingโ
<?php
$nums = [3, 1, 4, 1, 5, 9];
sort($nums); // ascending
rsort($nums); // descending
print_r($nums);
?>
Searchingโ
<?php
$fruits = ["Apple", "Banana", "Cherry"];
echo in_array("Banana", $fruits); // 1 (true)
echo array_search("Cherry", $fruits); // 2 (index)
?>
Adding & Removingโ
<?php
$arr = [1, 2, 3];
array_push($arr, 4, 5); // add to end โ [1,2,3,4,5]
array_pop($arr); // remove last โ [1,2,3,4]
array_unshift($arr, 0); // add to start โ [0,1,2,3,4]
array_shift($arr); // remove first โ [1,2,3,4]
print_r($arr);
?>
Slicingโ
<?php
$arr = [10, 20, 30, 40, 50];
$slice = array_slice($arr, 1, 3); // start at index 1, take 3
print_r($slice); // [20, 30, 40]
?>
Mergingโ
<?php
$a = [1, 2, 3];
$b = [4, 5, 6];
$merged = array_merge($a, $b);
print_r($merged); // [1, 2, 3, 4, 5, 6]
?>
Unique Valuesโ
<?php
$arr = [1, 2, 2, 3, 3, 3];
$unique = array_unique($arr);
print_r($unique); // [1, 2, 3]
?>
Mapping and Filteringโ
<?php
$nums = [1, 2, 3, 4, 5];
// map: double each value
$doubled = array_map(fn($n) => $n * 2, $nums);
print_r($doubled); // [2, 4, 6, 8, 10]
// filter: keep only even numbers
$evens = array_filter($nums, fn($n) => $n % 2 === 0);
print_r($evens); // [2, 4]
?>
Reduceโ
<?php
$nums = [1, 2, 3, 4, 5];
$sum = array_reduce($nums, fn($carry, $n) => $carry + $n, 0);
echo $sum; // 15
?>
Checking if Variable is an Arrayโ
<?php
$data = [1, 2, 3];
if (is_array($data)) {
echo "It's an array!";
}
?>
Converting Between Arrays and Stringsโ
<?php
// Array to string
$fruits = ["Apple", "Banana", "Cherry"];
echo implode(", ", $fruits); // Apple, Banana, Cherry
// String to array
$str = "one,two,three";
$arr = explode(",", $str);
print_r($arr); // [one, two, three]
?>
Finished reading? Mark this topic as complete.