Recap - Key-Value Data Store
Coddy PHP 여정의 기초 섹션에 포함된 레슨 — 71개 중 46번째.
챌린지
쉬움JSON 객체를 포함한 입력 줄을 읽으세요. 이는 연락처 정보를 나타냅니다 (예: {"name": "Alice", "phone": "555-1234"}).
그 다음 다음 입력을 읽으세요:
- 문자열
keyToCheck- 존재 여부를 확인할 키 - 문자열
keyToModify- 업데이트할 기존 키 - 문자열
newValue-keyToModify에 대한 새 값 - 문자열
newKey- 추가할 새 키 - 문자열
newKeyValue-newKey에 대한 값
JSON 입력으로부터 연관 배열을 생성한 후, 다음 작업을 수행하세요:
keyToCheck이 존재하는지 확인하세요. 존재하면Found를 출력하고, 그렇지 않으면Not found를 출력하세요.keyToModify의 값을newValue로 수정하세요.newKey와newKeyValue를 사용하여 새로운 키-값 쌍을 추가하세요.keyToModify의 업데이트된 값을 출력하세요.newKey의 값을 출력하세요.
각 결과를 별도의 줄에 출력하세요.
예제:
입력값이 {"name": "Alice", "phone": "555-1234"}, email, name, Bob, city, 및 Paris인 경우, 출력은 다음과 같아야 합니다:
Not found
Bob
Paris입력값이 {"product": "Laptop", "price": 999}, price, product, Tablet, stock, 및 25인 경우, 출력은 다음과 같아야 합니다:
Found
Tablet
25직접 해보기
<?php
// JSON 입력을 읽고 연관 배열로 변환
$jsonInput = trim(fgets(STDIN));
$contact = (array)json_decode($jsonInput, true);
// 다른 입력 읽기
$keyToCheck = trim(fgets(STDIN));
$keyToModify = trim(fgets(STDIN));
$newValue = trim(fgets(STDIN));
$newKey = trim(fgets(STDIN));
$newKeyValue = trim(fgets(STDIN));
// TODO: 아래에 코드 작성
// 1. keyToCheck가 존재하는지 확인하고 "Found" 또는 "Not found" 출력
// 2. keyToModify의 값을 newValue로 수정
// 3. newKey와 newKeyValue를 사용하여 새로운 키-값 쌍 추가
// 4. keyToModify의 업데이트된 값 출력
// 5. newKey의 값 출력
?>기초의 모든 레슨
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic7Arrays Part 2 - Associative
Intro to Associative ArraysAccessing Values by KeyModifying Values by KeyAdding New Key-Value PairsCheck if Key ExistsRecap - Key-Value Data Store2Variables 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