Answer the question
In order to leave comments, you need to log in
PHP Populate an array in increments of the existing values in the array?
Hello!
In the presence of an array of 14 elements, the elements of the array contain a void. Numbering of keys from 8th to 22nd.
When an element appears in the array, for example, the entry 12:30 appears in the key 12, then you need to fill in the empty cells in increments of 00:30 up and down.
How is this possible to implement?
PS It is also possible that the array contains two or three values, and you need to fill in the cells between them.
Answer the question
In order to leave comments, you need to log in
if on the knee
$arr = [
8 => '',
9 => '',
10 => '',
11 => '',
12 => '12:30',
13 => '',
14 => '',
15 => '',
16 => '',
17 => '',
18 => '',
19 => '',
20 => '',
21 => '',
22 => '',
];
$values = array_flip(array_filter($arr));
$key = array_pop($values);
$dt = \DateTime::createFromFormat('H:i', $arr[$key]);
foreach ($arr as $k => $v) {
if (empty($v)) {
$v = clone $dt;
if ($k < $key) {
$v->sub(new \DateInterval('PT' . (($key-$k) * 30) . 'M'));
} else {
$v->add(new \DateInterval('PT' . (($k-$key) * 30) . 'M'));
}
$arr[$k] = $v->format('H:i');
}
}
There are no "cells" in an associative array. Give an example of arrays in the form it was / became, according to your description it is not clear what you want to do.
for example, the entry 12:30 appears in the key 12, then you need to fill in the empty cells in increments of 00:30 up and down.
$i = 12;
$array[$i-1] = ...;
$array[$i+1] = ...;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question