M
M
mrserg41022018-08-29 20:47:06
PHP
mrserg4102, 2018-08-29 20:47:06

How to add missing elements to PHP array?

Hello! There is an array with data for the class schedule. Each element is an array with the name of the day and the time span. Since classes are not every day, there are no elements with some days (For example, classes are on Mon, Wed, Fri). The idea is to add elements to the array with the name of the day (without time), so that the days always go in order from Monday to Saturday, and even if the first day is Tuesday, or Wednesday could be formatted to start with Monday.
array:6 [▼
0 => array:2 [▼
"day" => "Monday"
"time" => "14:30-15.50"
]
1 => array:2 [▼
"day" => "Wednesday"
"time" => "14:30-15.50"
"time" => "14:30-15.50"
]
3 => array:2 [▼
"day" => "Monday"
"time" => "17:30-18:20"
]
4 => array:2 [▼
"day" => "Wednesday"
"time" => "17:30 -18:20"
]
5 => array:2 [▼
"day" => "Friday"
"time" => "17:30 –18:20"
]
]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Korobkov, 2018-08-29
@BorisKorobkov

1. add missing days: foreach ... if (!isset...) add.
2. sort by days of the week: usort

C
chibitko, 2018-08-31
@chibitko

Use an associative array

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question