New: Complete Beginner's Guide to Coding is now available in Premium
Updated: Indian Govt Exam roadmaps now include salary breakdowns & timelines
Tip: Use the Career Hub to explore all career paths in one place
Tutorials PHP PHP Arrays

PHP Arrays

PHP

Arrays store multiple values in a single variable. PHP has two types:

  • Indexed arrays — numbered keys (0, 1, 2...)
  • Associative arrays — named keys ("name", "age"...)

Arrays are the most used data structure in PHP. Master them!

Example — PHP
<?php
// Indexed array
$colors = ["Red", "Green", "Blue"];
echo $colors[0]; // Red
echo "<br>";
echo "Total colors: " . count($colors);

echo "<br><br>";

// Associative array
$student = [
    "name" => "Priya",
    "age" => 16,
    "class" => "10th",
    "marks" => 92
];

echo "Name: " . $student["name"] . "<br>";
echo "Class: " . $student["class"] . "<br>";
echo "Marks: " . $student["marks"] . "%";
?>
Result
Red
Total colors: 3

Name: Priya
Class: 10th
Marks: 92%

Interview Questions — PHP Arrays

5 questions commonly asked in interviews

Q1 What is an array in PHP? Beginner

An array is a variable that can store multiple values in a single variable.

Q2 What are the types of arrays in PHP? Beginner

PHP has indexed arrays, associative arrays, and multidimensional arrays.

Q3 What is an indexed array? Beginner

An indexed array uses numeric indexes starting from 0.

Q4 What is an associative array? Intermediate

An associative array uses named keys instead of numeric indexes.

Q5 What is a multidimensional array? Intermediate

A multidimensional array contains one or more arrays inside another array.

Frequently Asked Questions

Common doubts about PHP Arrays

Yes, PHP arrays can store strings, numbers, booleans, objects, and other arrays.

Use the count() function.

Use $array[] = value; or array_push().

Yes, associative arrays use string keys.

Yes, foreach is commonly used to loop arrays.

Test Your Knowledge

5 questions · Earn 50 XP

0 / 5
Q1 Which function counts array items?
Q2 Indexed array starts from?
Q3 Which array uses named keys?
Q4 Which loop is commonly used for arrays?
Q5 Which function adds item at end?

More on PHP Arrays

Cheatsheet, tips, resources & what to learn next

Quick Cheatsheet

Indexed Array
$colors = [\"red\", \"green\", \"blue\"];
Associative Array
$user = [\"name\" => \"John\", \"age\" => 25];
Access Item
echo $colors[0];
Foreach Array
foreach($colors as $color){ echo $color; }
Count Array
echo count($colors);

Pro Tips

1

Use indexed arrays for simple lists.

2

Use associative arrays for key-value data.

3

Use foreach for clean array iteration.

4

Use print_r() to debug arrays.

5

Keep multidimensional arrays well structured.

Up Next

PHP Functions PHP Forms PHP Sessions PHP MySQL PHP File Upload