Lesson 13: PHP Array Types
Have a Question?
Latest Articles

Lesson 13: PHP Array Types
Arrays are essential data types in PHP. They allow you to store multiple values in a single variable. In this article, we'll explore the main types of arrays in PHP with examples.
Indexed Array
Indexed arrays use numeric keys starting from 0. You can create them in two ways:
$colors = array("Red", "Green", "Blue");
// or
$colors[] = "Red";
$colors[] = "Green";
$colors[] = "Blue";
Accessing an element:
echo $colors[0]; // Output: Red
Associative Array
Associative arrays use named keys instead of numeric indexes:
$ages = array(
"Ahmed" => 25,
"Sara" => 30,
"Mohamed" => 28
);
echo $ages["Sara"]; // Output: 30
This type is useful for mapping related data, such as names and ages.
Multidimensional Array
This is an array that contains one or more arrays. It's perfect for structured data like tables.
$students = array(
array("Ahmed", 25, "Engineering"),
array("Sara", 23, "Computer Science"),
array("Mohamed", 24, "Commerce")
);
echo $students[0][0]; // Output: Ahmed
When to Use Each Type
Use Indexed Arrays for simple lists.
Use Associative Arrays to map related data.
Use Multidimensional Arrays for tabular or complex structures.
Conclusion
Understanding array types in PHP is crucial for every developer. They offer flexibility in storing and managing data. Try each type in your code today to gain hands-on experience.
Important links Portfolio
Share with your friends