PHP Arrays Explained (Indexed, Associative & Multidimensional with Examples)

Introduction

In PHP, arrays allow you to store multiple values in a single variable. Instead of creating multiple variables, you can group related data together and manage it efficiently.

Arrays are widely used in real-world applications, especially in WordPress development, where they help manage posts, users, and dynamic content.

🎯 What You’ll Learn

  • What arrays are in PHP
  • Types of arrays (indexed, associative, multidimensional)
  • How to loop through arrays
  • Common array functions
  • Real-world and WordPress examples

What Is an Array?

An array is a special variable that can hold more than one value at a time.

Example

$colors = array("Red", "Green", "Blue");

Here, $colors stores three values inside one variable.

⚡ PHP Arrays (Quick Overview)

  • Store multiple values in one variable
  • Support different data types
  • Used in loops and conditions
  • Essential for dynamic websites

Types of Arrays in PHP

PHP supports three types of arrays:

  1. Indexed Arrays
  2. Associative Arrays
  3. Multidimensional Arrays

Let’s understand each one.

1. Indexed Arrays

Indexed arrays use numeric indexes, starting from 0.

Example

$fruits = array("Apple", "Banana", "Mango");

echo $fruits[0];

Output:

Apple

Loop Through an Indexed Array

$fruits = array("Apple", "Banana", "Mango");

foreach ($fruits as $fruit) {
    echo $fruit . "<br>";
}

2. Associative Arrays

Associative arrays use named keys instead of numbers.

Example

$user = array(
    "name" => "Divyesh",
    "role" => "Admin",
    "age"  => 25
);

echo $user["name"];

Output:

Divyesh

Loop Through an Associative Array

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

3. Multidimensional Arrays

A multidimensional array contains arrays inside an array.

Example

$students = array(
    array("Rahul", 20),
    array("Anita", 22),
    array("Rohit", 21)
);

echo $students[0][0];

Output:

Romil

🔄 When to Use Each Array Type

  • Use indexed arrays for simple lists (e.g., products, colors)
  • Use associative arrays for structured data (e.g., user details)
  • Use multidimensional arrays for complex datasets (e.g., database records)

Loop Through a Multidimensional Array

foreach ($students as $student) {
    echo $student[0] . " - " . $student[1] . "<br>";
}

Common Array Functions

count()

Returns the number of elements in an array.

echo count($fruits);

array_push()

Adds an element to the end of an array.

array_push($fruits, "Orange");

array_pop()

Removes the last element from an array.

array_pop($fruits);

in_array()

Checks if a value exists in an array.

if (in_array("Apple", $fruits)) {
    echo "Apple found";
}

Real-Life Example

$menu = array("Home", "About", "Tutorials", "Contact");

foreach ($menu as $item) {
    echo "<li>$item</li>";
}

Arrays like this are commonly used to:

  • Build menus
  • Display posts
  • Handle form data

Let’s see some more examples

1️⃣ Add Array Sorting Examples (Very Practical)

Sorting Indexed Arrays

<?php
$numbers = [4, 1, 3, 2];
sort($numbers);

foreach ($numbers as $num) {
    echo $num . "<br>";
}
?>

Sorting Associative Arrays

<?php
$ages = ["Div" => 25, "Alex" => 30, "Sam" => 20];
asort($ages); // sort by value

foreach ($ages as $name => $age) {
    echo $name . ": " . $age . "<br>";
}
?>

Explaination:

  • sort() – sorts values
  • asort() – sorts associative array by values
  • ksort() – sorts by keys

2️⃣ Looping Through Arrays

<?php
$colors = ["Red", "Green", "Blue"];

for ($i = 0; $i < count($colors); $i++) {
    echo $colors[$i] . "<br>";
}
?>
  • Use foreach → best for arrays
  • Use for → when index control is needed

You can loop through arrays using foreach (learn more in our PHP Loops tutorial).

3️⃣ Array Search & Check Examples

<?php
$fruits = ["Apple", "Banana", "Orange"];

if (in_array("Banana", $fruits)) {
    echo "Banana is in the list";
}
?>
<?php
$user = ["name" => "Div", "role" => "admin"];

if (array_key_exists("role", $user)) {
    echo "Role key exists";
}
?>

4️⃣ WordPress Array Example (Real-World Use)

In WordPress development, many functions return arrays (such as posts, users, and settings). Developers loop through these arrays to display dynamic content.

<?php
$posts = get_posts(['numberposts' => 3]);

foreach ($posts as $post) {
    setup_postdata($post);
    echo "<h3>" . get_the_title() . "</h3>";
}
wp_reset_postdata();
?>

Explain:

  • Functions return arrays
  • You loop over them
  • This is common in themes/plugins

Common Mistakes

  • Trying to echo an array directly
  • Using wrong index keys
  • Forgetting quotes around string keys
  • Accessing non-existing indexes
  • Confusing associative vs indexed arrays

Practice Tasks

Practice these tasks to improve your understanding of PHP arrays.

  • Task 1: Create an indexed array of 5 colors and print them using foreach.
  • Task 2: Create an associative array for a user (name, email, role) and display each value.
  • Task 3: Create a multidimensional array of 2 users and display their names.
  • Challenge: Check if a value exists in an array using in_array().

Conclusion

PHP arrays are essential for storing and managing multiple values efficiently. In this tutorial, you learned:

  • Different types of arrays
  • How to loop through arrays
  • Common array functions
  • Real-world usage in PHP and WordPress

Arrays are widely used in dynamic applications, making them a fundamental concept for every PHP developer.

Next learn how to handle user input with PHP Forms (GET & POST)

Related Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *