Adding a New Event
Part of the Logic & Flow section of Coddy's PHP journey — lesson 60 of 68.
Challenge
EasyYou will receive two inputs: a JSON string representing the existing events array from the previous lesson, and three values on the second line separated by spaces representing a new event's name, date, and location.
The first input will be a JSON string in this format: [{"name":"Team Meeting","date":"2024-04-15","location":"Office"},{"name":"Conference","date":"2024-05-20","location":"Convention Center"}]
The second input will be three space-separated values in this format: Workshop 2024-06-10 Library
Read both inputs. Decode the JSON string into an events array. Then, parse the second input to extract the event name, date, and location. Create a new associative array for this event with keys "name", "date", and "location", and add it to the events array using array_push().
After adding the new event, iterate through all events in the array and print each one in the following format:
Event: [name] on [date] at [location]
Print each event on a separate line, in the order they appear in the array.
Input format:
- First line: A JSON string representing the existing events array
- Second line: Three space-separated values representing the new event's name, date, and location (example:
Workshop 2024-06-10 Library)
Expected output: Each event (including the newly added one) printed on a separate line in the format: Event: [name] on [date] at [location]
Try it yourself
<?php
// Read the JSON string from the first input
$jsonInput = trim(fgets(STDIN));
// Read the new event details from the second input
$newEventInput = trim(fgets(STDIN));
// Decode the JSON string into an array
$events = (array)json_decode($jsonInput, true);
// Parse the new event details (name, date, location)
list($name, $date, $location) = explode(' ', $newEventInput);
// TODO: Write your code below
// Create a new event array and add it to the events array using array_push()
// Then iterate through all events and print them in the required format
?>All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting6Higher-Order Array Functions
Transforming with 'array_map''array_map' with Custom FuncFiltering with 'array_filter''array_filter' with Custom FunReducing with 'array_reduce'Walking with 'array_walk'Recap: Data Processing9Event Scheduler
Event DataAdding a New Event