Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 10

Arrays in PHP (Indexed, Associative, Multidimensional)

In this lesson: Learn all 3 PHP array types with create, access, loop and print_r output — plus practical student-marks example. Hindi & English.

What is an array

An array is one variable that holds many values together, instead of making a separate variable for each. Want to store 30 students' marks? One array does it. PHP has two main kinds: indexed (numbered) and associative (named keys).

💡 Think of an array as a row of boxes. Each box has a key (its position or name) and a value inside.

Indexed arrays

Keys are numbers, starting at 0.

<?php
  $fruits = ["Apple", "Mango", "Banana"];
  echo $fruits[0];   // Apple
  echo $fruits[2];   // Banana
  echo count($fruits); // 3
?>
Apple Banana 3

The first item is [0], not [1]. count() tells you how many items are in the array.

Associative arrays

Here you choose meaningful text keys instead of numbers.

<?php
  $student = [
      "name"  => "Aman",
      "class" => 12,
      "marks" => 88
  ];
  echo $student["name"];   // Aman
  echo $student["marks"];  // 88
?>
Aman 88

Associative arrays are perfect when each value needs a label, like a record with name, class and marks.

Looping with foreach

foreach is the cleanest way to go through an array.

<?php
  $marks = ["Math" => 90, "Science" => 85];
  foreach ($marks as $subject => $score) {
      echo "$subject: $score\n";
  }
?>
Math: 90
Science: 85

For a plain list use foreach ($arr as $value); for key-value pairs use foreach ($arr as $key => $value).

Multidimensional arrays

An array of arrays — like a table.

<?php
  $students = [
      ["Aman", 88],
      ["Diya", 92]
  ];
  echo $students[1][0];   // Diya
  echo $students[1][1];   // 92
?>
Diya 92

$students[1][0] means: row 1 (second student), column 0 (name). This is how you store rows of related data.

Useful array functions

FunctionWhat it does
count($a)How many items
$a[] = "x"Add item at the end
in_array("x", $a)Check if a value exists
sort($a)Sort values ascending
array_keys($a)Get all keys

Common mistakes

  • Forgetting arrays start at 0. The first item is index 0, so a 3-item array has no index 3.
  • Using an undefined key. $a["xyz"] when "xyz" was never set gives a warning.
  • Using a for loop where foreach is cleaner. For associative arrays, foreach avoids key mistakes.
  • Mixing up value and key in foreach. The order is $key => $value, not the reverse.

Summary

  • An array holds many values in one variable.
  • Indexed arrays use numbers from 0; associative use text keys.
  • Loop with foreach — cleanest for both kinds.
  • Multidimensional arrays store rows of related data.
  • Handy functions: count, in_array, sort, array_keys.

Next, loop over arrays with PHP loops, or process array text with string functions.

Frequently Asked Questions

What is the difference between an indexed and an associative array?
An indexed array uses numbers as keys, starting at 0 (like $a[0]). An associative array uses your own text keys (like $a["name"]). Use indexed for a simple list, and associative when each value needs a meaningful label.
Why do PHP arrays start at index 0?
The first item is at position 0, the second at 1, and so on. This is standard in most programming languages. So an array of 3 items has indexes 0, 1 and 2 — not 1, 2, 3.
How do I loop through an array?
Use foreach. For a simple list: foreach ($arr as $value). For key-value pairs: foreach ($arr as $key => $value). foreach handles the counting for you.
How do I add an item to an array?
Use $arr[] = "new" to add to the end, or array_push($arr, "new"). For associative arrays, just assign a key: $arr["city"] = "Delhi".
What is a multidimensional array?
It is an array whose items are themselves arrays — like a table of rows and columns. For example a list of students where each student is an array of name and marks.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicString Functions Your progress: 10 / 50 (20%) · Lesson 11 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free