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

التصفية حسب حالة الإكمال

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

challenge icon

التحدي

سهل

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

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

بعد بناء مصفوفة $todos، وتحديد المهمة المحددة كمكتملة، وإزالة المهمة عند الفهرس المعطى، قم بتصفية المهام المتبقية بناءً على مدخل التصفية:

  • إذا كان الفلتر هو completed، فاعرض فقط المهام التي تكون فيها قيمة "completed" هي true
  • إذا كان الفلتر هو pending، فاعرض فقط المهام التي تكون فيها قيمة "completed" هي false

قم بالمرور عبر المهام المتبقية واطبع فقط المهام التي تطابق الفلتر، باستخدام نفس التنسيق:

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

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

مثال:

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

- Walk the dog (Done)

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

- Buy groceries (Pending)
- Call mom (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));

// Read the index of the task to remove
$removeIndex = (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;

// Remove the task at the specified removal index
array_splice($todos, $removeIndex, 1);

// 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