Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

إزالة مهمة

جزء من قسم Fundamentals في رحلة PHP على Coddy — الدرس 58 من 71.

challenge icon

التحدي

سهل

في الدرس السابق، قمت بتمييز مهمة كمكتملة. الآن، أضف القدرة على إزالة مهمة من القائمة تماماً.

اقرأ أربعة أسطر من المدخلات لوصف المهام (ثلاث مهام أولية بالإضافة إلى مهمة واحدة جديدة). ثم اقرأ مدخلاً خامساً: عدد صحيح يمثل فهرس (index) المهمة المراد تمييزها كمكتملة. وأخيراً، اقرأ مدخلاً سادساً: عدد صحيح يمثل فهرس المهمة المراد إزالتها من القائمة.

بعد بناء مصفوفة $todos بجميع المهام الأربع وتمييز المهمة المحددة كمكتملة، قم بإزالة المهمة عند فهرس الإزالة المحدد باستخدام array_splice().

تقوم وظيفة array_splice() بإزالة العناصر من المصفوفة:

<?php
array_splice($array, $index, 1); // Removes 1 element at $index
?>

ثم قم بالمرور عبر المهام المتبقية واطبع كل واحدة منها بنفس التنسيق السابق:

  • إذا كانت المهمة مكتملة: - [task description] (Done)
  • إذا كانت المهمة معلقة: - [task description] (Pending)

اطبع كل مهمة في سطر منفصل.

مثال:

إذا كانت المدخلات هي Buy groceries، و Walk the dog، و Finish homework، و Call mom، و 1، و 2، فيجب أن يكون المخرج:

- Buy groceries (Pending)
- Walk the dog (Done)
- Call mom (Pending)

إذا كانت المدخلات هي Clean room، و Read book، و Exercise، و Cook dinner، و 0، و 0، فيجب أن يكون المخرج:

- Read book (Pending)
- Exercise (Pending)
- Cook dinner (Pending)

جرّب بنفسك

<?php
// Read three task descriptions
$task1 = trim(fgets(STDIN));
$task2 = trim(fgets(STDIN));
$task3 = trim(fgets(STDIN));

// Read the new task to add
$newTask = trim(fgets(STDIN));

// Read the index of the task to mark as complete
$completeIndex = (int)trim(fgets(STDIN));

// Create a $todos array containing three task arrays
// Each task should be an associative array with "task" and "completed" keys
$todos = [
    ["task" => $task1, "completed" => false],
    ["task" => $task2, "completed" => false],
    ["task" => $task3, "completed" => false]
];

// Add the new task to the end of the array
$todos[] = ["task" => $newTask, "completed" => false];

// Mark the task at the specified index as complete
$todos[$completeIndex]["completed"] = true;

// Loop through the array and print each task in the formatted list
foreach ($todos as $todo) {
    if ($todo["completed"]) {
        echo "- " . $todo["task"] . " (Done)\n";
    } else {
        echo "- " . $todo["task"] . " (Pending)\n";
    }
}
?>

جميع دروس Fundamentals