November 27, 2026
Explain the different types of arrays in PHP with examples?
Array with numeric index or key
In this kind of array, keys of array elements are always numeric. The example is mentioned below:
Code
<?php
$myArray[0] = "Mango";
$myArray[1] = "Guava";
$myArray[2] = "Orange";
?>
Associative Array
In this kind of array, keys of an array can be a string or a key of an array associative with value. The example of associative array is mentioned below:
Code
<?php
$marks["John"] = 90;
$marks["Neil"] = 40;
$marks["Jack"] = 60;
$marks["Edge"] = 70;
echo "John's marks are - " . $marks["John"] . "
";
echo "Neil's marks are - " . $marks["Neil"] . "
";
echo "Jack's marks are - " . $marks["Jack"] . "
";
echo "Edge's marks are - " . $marks["Edge"];
?>
In above mentioned example we have created an associative array which contained a list of student name and their marks.
FAQ
1. What is the difference between indexed and associative arrays in PHP?
An indexed array uses numeric indexes to access elements, whereas an associative array uses named keys to access elements.
2. Can an associative array contain both string and numeric keys in PHP?
Yes. PHP arrays can contain both string and integer keys in the same array.