작업 제거
Coddy PHP 여정의 기초 섹션에 포함된 레슨 — 71개 중 58번째.
챌린지
쉬움이전 수업에서 작업을 완료로 표시했습니다. 이제 목록에서 작업을 완전히 제거하는 기능을 추가하세요.
작업 설명을 위한 네 줄의 입력을 읽으세요 (초기 세 작업과 하나의 새 작업). 그런 다음 다섯 번째 입력을 읽으세요: 완료로 표시할 작업의 인덱스를 나타내는 정수. 마지막으로 여섯 번째 입력을 읽으세요: 목록에서 제거할 작업의 인덱스를 나타내는 정수.
모든 네 작업으로 $todos 배열을 구성하고 지정된 작업을 완료로 표시한 후, array_splice()를 사용하여 주어진 제거 인덱스의 작업을 제거하세요.
array_splice() 함수는 배열에서 요소를 제거합니다:
<?php
array_splice($array, $index, 1); // Removes 1 element at $index
?>그런 다음 남은 작업을 순회하며 이전과 같은 형식으로 각 작업을 출력하세요:
- 작업이 완료된 경우:
- [task description] (Done) - 작업이 대기 중인 경우:
- [task description] (Pending)
각 작업을 별도의 줄에 출력하세요.
Example:
입력값이 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
// 작업 컬렉션
$tasks = [
[
"description" => "Buy groceries",
"completed" => false
],
[
"description" => "Walk the dog",
"completed" => true
],
[
"description" => "Read a book",
"completed" => false
],
[
"description" => "Exercise",
"completed" => true
],
[
"description" => "Clean house",
"completed" => false
]
];
// 입력 읽기
$taskIndex = trim(fgets(STDIN));
// TODO: 여기에 코드를 작성하세요
// 작업 인덱스를 정수로 변환
// unset()을 사용하여 작업 제거
// array_values()를 사용하여 배열 재인덱싱
// 제거된 작업과 남은 작업 표시
?>기초의 모든 레슨
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators