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

التعامل مع حالات القائمة الفارغة

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

challenge icon

التحدي

سهل

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

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

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

قبل عرض المهام المصفاة، تحقق مما إذا كانت هناك أي مهام تطابق الفلتر:

  • إذا لم تطابق أي مهام الفلتر، اطبع: No [filter] tasks found. (حيث [filter] هي إما completed أو pending)
  • إذا كانت المهام تطابق الفلتر، فاعرضها باستخدام نفس التنسيق السابق

تنسيق المخرجات للمهام المطابقة:

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

المثال 1:

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

No completed tasks found.

المثال 2:

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

- Buy groceries (Done)

المثال 3:

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

- Finish homework (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));

// Read the filter type (completed or pending)
$filter = 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 that matches the filter
foreach ($todos as $todo) {
    if ($filter === "completed" && $todo["completed"]) {
        echo "- " . $todo["task"] . " (Done)\n";
    } elseif ($filter === "pending" && !$todo["completed"]) {
        echo "- " . $todo["task"] . " (Pending)\n";
    }
}
?>

جميع دروس Fundamentals