Skip to content

Instantly share code, notes, and snippets.

@walison17
Last active August 1, 2019 17:22
Show Gist options
  • Save walison17/544ef5bc112d557fa4328662688f6ea9 to your computer and use it in GitHub Desktop.
Save walison17/544ef5bc112d557fa4328662688f6ea9 to your computer and use it in GitHub Desktop.
<?php
public function distribuirLotes($resultSet)
{
$lotes = [];
foreach ($resultSet as $row) {
$lotes = array_merge($lotes, $this->organizarIntervalos($row));
}
return $lotes;
}
private function organizarIntervalos($row)
{
$sequencias = $row->sequencias;
sort($sequencias);
$ranges = [];
$lastIndex = $sequencias[count($sequencias) - 1];
$temp = [];
for ($i = $sequencias[0]; $i <= $lastIndex; $i++) {
if (in_array($i, $sequencias)) {
$temp[] = $i;
}
if ((! in_array($i, $sequencias) || $i == $lastIndex) && ! empty($temp)) {
$ranges[] = [
'evento_tipo_id' => $row->evento_tipo_id,
'evento_tipo' => $row->evento_tipo,
'lote_inicial' => $temp[0],
'lote_final' => end($temp)
];
$temp = [];
}
}
return $ranges;
}
<?php
function findRanges($numbers)
{
sort($numbers);
$ranges = [];
$lastIndex = $numbers[count($numbers) - 1];
$temp = [];
for ($i = $numbers[0]; $i <= $lastIndex; $i++) {
if (in_array($i, $numbers)) {
$temp[] = $i;
}
if ((! in_array($i, $numbers) || $i == $lastIndex) && ! empty($temp)) {
$ranges[] = ['min' => $temp[0], 'max' => end($temp)];
$temp = [];
}
}
return $ranges;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment