Arrays in PHP are used to store multiple values in a single variable. Instead of using separate variables for every value, arrays group them together efficiently.
<?php
$fruits = array("Apple", "Banana", "Mango");
echo $fruits[0]; // Apple
?>
<?php
$age = array("John" => 25, "Jane" => 30);
echo $age["Jane"]; // 30
?>
<?php
$students = array(
array("John", 20),
array("Jane", 22)
);
echo $students[1][0]; // Jane
?>
<?php
$colors = array("Red", "Green", "Blue");
foreach ($colors as $color) {
echo $color . "<br>";
}
?>
count() โ Get number of elementsarray_push() โ Add elementsarray_merge() โ Merge two arraysarray_keys() โ Get keysin_array() โ Check if value existsPractice Idea: Try making a list of students with names and grades using associative arrays.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!