$res = [];
array_walk(
$arr,
function($k, $v) use (&$res) {
// создаем любые переменные в любом количестве
// при необходимости пишем что-нибудь в $res
}
);
DELETE FROM `table` WHERE `idu` = 1 AND `time_end` NOT IN
(SELECT `time_end` FROM `table` ORDER BY `time_end` DESC LIMIT 3)
DELETE FROM `table` WHERE `idu` = 1 AND `time_end` <
(SELECT * FROM (SELECT `time_end` FROM `table` ORDER BY `time_end` DESC LIMIT 3) as T
ORDER BY `time_end` LIMIT 1)