Skip to content

PHP Tutorial Arrays Exercise

Practice: Array Exercises

Lesson 133 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Use the product list below for the first three exercises. Try each one yourself, then open the solution.

index.php
PHP
<?php
$products = [
    ["name" => "Notebook", "price" => 80, "category" => "paper", "stock" => 25],
    ["name" => "Pen", "price" => 12, "category" => "writing", "stock" => 100],
    ["name" => "Bag", "price" => 799, "category" => "bags", "stock" => 0],
    ["name" => "Marker", "price" => 45, "category" => "writing", "stock" => 30],
    ["name" => "Diary", "price" => 150, "category" => "paper", "stock" => 4],
];

Exercise 1: a product filter

Show the names of products that cost 100 or less and are in stock, sorted from the cheapest.

Show a possible solution
index.php
PHP
<?php
$products = [
    ["name" => "Notebook", "price" => 80, "category" => "paper", "stock" => 25],
    ["name" => "Pen", "price" => 12, "category" => "writing", "stock" => 100],
    ["name" => "Bag", "price" => 799, "category" => "bags", "stock" => 0],
    ["name" => "Marker", "price" => 45, "category" => "writing", "stock" => 30],
    ["name" => "Diary", "price" => 150, "category" => "paper", "stock" => 4],
];

$result = array_filter($products, fn($p) => $p["price"] <= 100 && $p["stock"] > 0);
usort($result, fn($a, $b) => $a["price"] <=> $b["price"]);

echo implode(", ", array_column($result, "name"));
Output
OUTPUT
Pen, Marker, Notebook

Exercise 2: group by category

Create an array where each key is a category and each value is the list of product names in it.

Show a possible solution
index.php
PHP
<?php
$products = [
    ["name" => "Notebook", "category" => "paper"],
    ["name" => "Pen", "category" => "writing"],
    ["name" => "Bag", "category" => "bags"],
    ["name" => "Marker", "category" => "writing"],
    ["name" => "Diary", "category" => "paper"],
];

$grouped = [];

foreach ($products as $product) {
    $grouped[$product["category"]][] = $product["name"];
}

foreach ($grouped as $category => $names) {
    echo $category . ": " . implode(", ", $names) . "\n";
}
Output
OUTPUT
paper: Notebook, Diary
writing: Pen, Marker
bags: Bag

Exercise 3: inventory value

Calculate the total value of the stock (price times stock for every product) using array_reduce().

Show a possible solution
index.php
PHP
<?php
$products = [
    ["price" => 80, "stock" => 25],
    ["price" => 12, "stock" => 100],
    ["price" => 799, "stock" => 0],
    ["price" => 45, "stock" => 30],
    ["price" => 150, "stock" => 4],
];

$value = array_reduce($products, fn($sum, $p) => $sum + $p["price"] * $p["stock"], 0);

echo $value;
Output
OUTPUT
5150

Exercise 4: the most common word

Find the word that appears most often in "the cat and the dog and the bird".

Show a possible solution
index.php
PHP
<?php
$text = "the cat and the dog and the bird";

$counts = array_count_values(explode(" ", $text));
arsort($counts);

echo array_key_first($counts) . " (" . reset($counts) . " times)";
Output
OUTPUT
the (3 times)

Exercise 5: unique sorted tags

From ["php", "Web", "php", "css", "web", "CSS"], produce a sorted list of unique tags, ignoring case.

Show a possible solution
index.php
PHP
<?php
$tags = ["php", "Web", "php", "css", "web", "CSS"];

$unique = array_unique(array_map("strtolower", $tags));
sort($unique);

echo implode(", ", $unique);
Output
OUTPUT
css, php, web