MasterPHP.in
PHP Tutorial

Real Use Cases of Arrays in PHP


So far, you’ve learned how arrays work and how to perform operations on them. But in real-world development, arrays are not just data structures — they are everywhere.

From database results to API responses, arrays power most PHP applications.

In this lesson, you’ll work with real-world scenarios where arrays are used in practical ways.


Why Arrays Matter in Real Projects

In real applications, arrays are used for:

  • storing database records
  • handling API responses
  • managing dynamic content
  • filtering and transforming data

If you understand arrays well, you can handle most backend tasks in PHP.


Use Case 1: Displaying Product Data

Scenario

You are building an eCommerce page that displays products.

Example

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

foreach ($products as $product) {
    echo $product["name"] . " - ₹" . $product["price"] . "<br>";
}

Output:

Laptop - ₹50000
Phone - ₹20000
Mouse - ₹1000


Use Case 2: Filtering Data (Important)

Scenario

You want to display only expensive products.

Using foreach

foreach ($products as $product) {
    if ($product["price"] > 10000) {
        echo $product["name"] . "<br>";
    }
}

Output:

Laptop
Phone

Using array_filter() (Modern Approach)

$expensive = array_filter($products, fn($p) => $p["price"] > 10000);

print_r($expensive);

👉 Cleaner and more scalable than manual loops.


Use Case 3: Handling API Response Data

Scenario

You receive structured data from an API.

Example

$response = [
    "status" => "success",
    "data" => [
        ["name" => "Rohan", "age" => 22],
        ["name" => "Amit", "age" => 25]
    ]
];

foreach ($response["data"] as $user) {
    echo $user["name"] . "<br>";
}

Output:

Rohan
Amit


Use Case 4: Grouping Data (Advanced)

Scenario

Group users based on their role.

Example

$users = [
    ["name" => "Rohan", "role" => "admin"],
    ["name" => "Amit", "role" => "user"],
    ["name" => "Priya", "role" => "admin"]
];

$grouped = [];

foreach ($users as $user) {
    $grouped[$user["role"]][] = $user["name"];
}

print_r($grouped);

Output:

Array (
    [admin] => Array ( [0] => Rohan [1] => Priya )
    [user] => Array ( [0] => Amit )
)


Common Mistakes in Real Use

1. Not Checking If Key Exists

echo $user["email"]; // Warning if not set

Correct:

if (isset($user["email"])) {
    echo $user["email"];
} else {
    echo "Email not found";
}

2. Assuming API Structure Without Validation

echo $response["data"][0]["name"];

Problem:

Breaks if data is missing or empty.

Correct:

if (!empty($response["data"])) {
    echo $response["data"][0]["name"] ?? "Not found";
}


Practice Exercise

Task 1 (Easy)

Use this array and print all product names:

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

Expected Output:

Laptop
Phone

Task 2 (Medium)

Use this array and print only products above ₹10000:

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

Expected Output:

Laptop
Phone

Task 3 (Hard)

Use this array and group users by role:

$users = [
    ["name" => "Rohan", "role" => "admin"],
    ["name" => "Amit", "role" => "user"],
    ["name" => "Priya", "role" => "admin"]
];

Expected Output:

Array (
    [admin] => Array ( [0] => Rohan [1] => Priya )
    [user] => Array ( [0] => Amit )
)


Summary

In this lesson, you learned how arrays are used in real-world PHP applications — including displaying data, filtering results, handling API responses, and grouping data.

These patterns are used in almost every backend system.

Share this tutorial