MasterPHP.in
PHP Tutorial

PHP Arrays Explained


Imagine you have 100 products in an online store. Would you create 100 separate variables?

$product1 = "Laptop";
$product2 = "Phone";
// ...

That quickly becomes impossible to manage.

Arrays solve this problem by allowing you to store multiple values in a single variable — and process them efficiently using loops and functions.

In this lesson, you’ll learn how arrays work in PHP, including indexed, associative, and multidimensional arrays.


What is an Array in PHP?

An array is a variable that can hold multiple values at once.


Creating Arrays in PHP

PHP provides two ways to create arrays:

// Modern syntax (recommended)
$colors = ["red", "blue", "green"];

// Older syntax (still widely used)
$colors = array("red", "blue", "green");

Both are equivalent — but [] is preferred in modern PHP.


Indexed Arrays

Indexed arrays store values with numeric indexes starting from 0.

Example

$colors = ["red", "blue", "green"];

echo $colors[0];

Output:

red

Understanding Indexes

IndexValue0red1blue2green

👉 PHP arrays start at 0, not 1.

Looping Through Indexed Array

foreach ($colors as $color) {
    echo $color . " ";
}

Output:

red blue green


Associative Arrays

Associative arrays use named keys instead of numbers.

Example

$user = [
    "name" => "Rohan",
    "age" => 22
];

echo $user["name"];

Output:

Rohan

Looping Associative Array

foreach ($user as $key => $value) {
    echo $key . ": " . $value . " ";
}

Output:

name: Rohan age: 22

Real-world connection:

Database rows, API responses, and form data all use associative arrays.


Multidimensional Arrays

A multidimensional array contains multiple arrays.

Example

$users = [
    ["Rohan", 22],
    ["Amit", 25]
];

echo $users[0][0];

Output:

Rohan

Associative Multidimensional Array

$users = [
    ["name" => "Rohan", "age" => 22],
    ["name" => "Amit", "age" => 25]
];

echo $users[1]["name"];

Output:

Amit


Modifying Arrays (Update & Delete)

Update Value

$colors[0] = "yellow";

Delete Value

unset($colors[1]);

👉 Note: unset() removes the element but does not re-index automatically.


Common Array Functions

Basic Functions

count($colors);        // Get total elements
array_push($colors, "black"); // Add element
array_pop($colors);    // Remove last element

Useful Functions (Important)

// Merge arrays
array_merge($a, $b);

// Check if value exists
in_array("red", $colors);

// Sort arrays
sort($colors);   // Ascending
rsort($colors);  // Descending

// Sort associative arrays by key
ksort($user);

// Re-index array
array_values($colors);

👉 These functions are used heavily in real-world PHP applications


Common Mistakes Beginners Make

1. Accessing Non-Existing Index


$colors = ["red", "blue"];

echo $colors[2]; // Warning: Undefined array key

Correct:

echo $colors[1]; // blue

2. Using Index Instead of Key

$user = ["name" => "Rohan"];

echo $user[0]; // Undefined

Correct:

echo $user["name"];

3. Incorrect Multidimensional Access

$users = [
    ["name" => "Rohan"]
];

echo $users["name"]; // Wrong

Correct:

echo $users[0]["name"];


Real-World Example

$products = [
    ["name" => "Laptop", "price" => 50000],
    ["name" => "Phone", "price" => 20000]
];

$total = 0;

foreach ($products as $product) {
    $total += $product["price"];
}

echo "Total: ₹" . $total;

Output:

Total: ₹70000


Practice Exercise

Task 1 (Easy)

Create an indexed array of 3 fruits and print the second fruit.

Expected Output:

Apple

Task 2 (Medium)

Create an associative array and loop through it to print all keys and values.

Expected Output:

name: Rohan age: 22

Task 3 (Hard)

Create a multidimensional array of products and calculate the total price using a loop.

Expected Output:

Total: ₹70000


Summary

In this lesson, you learned how arrays work in PHP, including indexed, associative, and multidimensional arrays. You also explored how to modify arrays, use built-in functions, and avoid common mistakes.

Arrays are a core concept in PHP and are used in almost every real-world application.

Share this tutorial