Created
January 19, 2017 16:50
-
-
Save anonymous/fcac6e1b1e042db542721a626fe6df3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// То что прилетит в $tasks | |
[ | |
[task_id] => '0', | |
[ttAfter] => '', | |
[ttText] => 'Первая строчка' | |
], | |
[ | |
[task_id] => '12612', | |
[ttAfter] => '0', | |
[ttText] => 'Вторая строчка' | |
], | |
[ | |
[task_id] => '24', | |
[ttAfter] => '12612', | |
[ttText] => 'Третья строчка' | |
], | |
[ | |
[task_id] => '1367', | |
[ttAfter] => '24', | |
[ttText] => 'Четвертая строчка' | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Делаем запрос к БД | |
$tasks = $db->select([ | |
'query' => 'task_id, ttText, ttExec, ttAfter FROM tasker_tasks WHERE user_id = :user_id AND ttGroup = :group', | |
'bind' => [ | |
':user_id' => $idOfUser, | |
':group' => $group, | |
] | |
]); | |
// Ищем первый элемент который ни за кем не следует | |
foreach ($tasks as $k => $v) { | |
if ($v['ttAfter'] == null) { | |
$tasksInRightOrder[] = $v; | |
$currentTaskID = $v['task_id']; | |
} | |
} | |
// Формируем новый массив в порядке "кто за кем следует" | |
$tic = count($tasks); | |
$tac = ''; | |
while ($tic != $tac) { | |
foreach ($tasks as $k => $v) { | |
if ($v['ttAfter'] == $currentTaskID) { | |
$tasksInRightOrder[] = $v; | |
$currentTaskID = $v['task_id']; | |
} | |
} | |
$tac = count($tasksInRightOrder); | |
} | |
// Вот он наш итоговый рассортированный по приоритетам массив | |
$tasksInRightOrder; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment