3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ ['id' => 7867867, 'lock' => 0, 'score' => 322, 'strtotime' => 16614713], ['id' => 7867867, 'lock' => 0, 'score' => 444, 'strtotime' => 16614613], ['id' => 7867867, 'lock' => 3, 'score' => 0, 'strtotime' => 16613713], ['id' => 7867867, 'lock' => 0, 'score' => 11, 'strtotime' => 16612713], ['id' => 7867867, 'lock' => 5, 'score' => 0, 'strtotime' => 16614413], ['id' => 7867867, 'lock' => 0, 'score' => 42, 'strtotime' => 16614113], ['id' => 7867867, 'lock' => 0, 'score' => 22, 'strtotime' => 16614013], ]; $maxIndex = count($array) - 1; for ($a = 0; $a < $maxIndex; ++$a) { if ($array[$a]['lock'] !== 0) { continue; // cannot move locked row } for ($b = 0; $b < $maxIndex; ++$b) { if ($array[$b]['lock'] !== 0) { continue; // cannot move locked row } // find next movable row for ($c = $b + 1; $c <= $maxIndex; ++$c) { if ($array[$c]['lock'] === 0) { break; // $c is index of non-locked row } } if ($c > $maxIndex) { break; // no more movable rows } // sort movable rows if ( $array[$b]['score'] > $array[$c]['score'] || ($array[$b]['score'] === $array[$c]['score'] && $array[$b]['strtotime'] > $array[$c]['strtotime']) ) { [$array[$b], $array[$c]] = [$array[$c], $array[$b]]; } } } var_export($array);

preferences:
27.86 ms | 404 KiB | 5 Q