CodeBlanks Logo Icon CodeBlanks

Completing tasks

PHP course · Beginner · Lesson 86/116

Use an associative array to mark a task as completed.

Marking a task as completed

We now have three tasks but all are pending. To mark a task as completed we set its done key to true. We find a task by its index: $tasks[0] is the first task, $tasks[1] the second.

To reach a key inside an associative array you use square brackets twice: $tasks[$index]["done"]. When you assign true to it, the task is done and listTasks shows it as [x].

We wrap this in a function too and call it as completeTask($tasks, $index). To handle an invalid index safely we check with isset.

The completeTask function

The function signature is function completeTask(array $tasks, int $index): array. The second parameter int $index takes the position of the task to complete.

In the body we first check with isset($tasks[$index]) whether a task exists at that index. If so we set $tasks[$index]["done"] = true. If not we do nothing; that way an invalid index does not turn into an error. Finally return $tasks; gives the array back.

Exercise Write the completeTask function and complete the first task

Define the completeTask function with the signature function completeTask(array $tasks, int $index): array. In the body check with if (isset($tasks[$index])); inside it write $tasks[$index]["done"] = true;. Then give the array back with return $tasks;. After the function call $tasks = completeTask($tasks, 0); to complete the first task and call listTasks($tasks); to print the list again. The last three output lines will then read 1. Buy bread [x], 2. Buy milk [ ], 3. Take out trash [ ].

Show example solution
function completeTask(array $tasks, int $index): array. Body: if (isset($tasks[$index])) { $tasks[$index]["done"] = true; } return $tasks;. Then $tasks = completeTask($tasks, 0); and listTasks($tasks);.
Example solution:
<?php
// Initialize the tasks array and add the first task
$tasks = [];
$tasks[] = ["title" => "Buy bread", "done" => false];
echo "Task count: " . count($tasks) . "\n";
// Define the addTask function and add two more tasks
function addTask(array $tasks, string $title): array {
    $tasks[] = ["title" => $title, "done" => false];
    return $tasks;
}
$tasks = addTask($tasks, "Buy milk");
$tasks = addTask($tasks, "Take out trash");
echo "Task count: " . count($tasks) . "\n";
// Write and call the listTasks function
function listTasks(array $tasks): void {
    foreach ($tasks as $i => $task) {
        $mark = "[ ]";
        if ($task["done"]) {
            $mark = "[x]";
        }
        echo ($i + 1) . ". " . $task["title"] . " " . $mark . "\n";
    }
}
listTasks($tasks);
// Write the completeTask function and complete the first task
function completeTask(array $tasks, int $index): array {
    if (isset($tasks[$index])) {
        $tasks[$index]["done"] = true;
    }
    return $tasks;
}
$tasks = completeTask($tasks, 0);
listTasks($tasks);

Test yourself

  1. What do you do to mark a task as 'completed'?

    • Remove the task from the array
    • Add a new task
    • Change the task's title
    • Set the task's 'done' key to true
    Show answer

    Set the task's 'done' key to true — Correct. Setting the done key to true is the right way to mark a task as completed.

  2. Assign true to the done key

    if (isset($tasks[$index])) {
        ____;
    }
    Show answer
    if (isset($tasks[$index])) {
        $tasks[$index]["done"] = true;
    }
  3. Match the associative array concepts

    $tasks[$index]
    picks the task
    "done"
    completion key
    true
    completed value
    isset(...)
    checks key existence

All lessons in Beginner