Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

12. Arrays & Types

Lesson 9 of 29 in the free PHP Programming notes on Siksha Sarovar, written by Rohit Jangra.

Arrays in PHP

An array is a special variable that can store multiple values in a single variable, instead of creating separate variables for each value.

Why Arrays are Needed?

Without arrays:

$student1 = "Amit";
$student2 = "Rohit";
$student3 = "Neha";

With arrays:

$students = array("Amit", "Rohit", "Neha");
Exam Definition: An array is a collection of similar or different data types stored under a single variable name.

---

Types of Arrays in PHP

PHP mainly supports three types of arrays:

  1. Indexed Array
  2. Associative Array
  3. Multidimensional Array

---

How to Create an Array

1. Indexed Array

Uses numeric index (0,1,2,...).

Syntax

$arrayName = array(value1, value2, value3);

Example

$colors = array("Red", "Green", "Blue");
echo $colors[0];   // Red

2. Associative Array

Uses named keys instead of numbers.

Syntax

$arrayName = array(key => value);

Example

$student = array(
   "name" => "Rohit",
   "age" => 21,
   "course" => "BCA"
);
echo $student["name"];
Real-time Use: Storing database records.

3. Multidimensional Array

An array containing one or more arrays.

Example

$students = array(
   array("Rohit", 21),
   array("Amit", 22),
   array("Neha", 20)
);

echo $students[1][0];   // Amit
Real-time Use: Storing table-like data.

Short Array Syntax

$numbers = [10, 20, 30];
Exam Tip: PHP supports both traditional and short array syntax.