PHP function to extract numbers from the list whose squares are exceed some threshold This is my naive approach:

$sample1 = [3, -20, 1, -19, -4, 6, 77, -1, 0, 6];
$sample3 = [4, 5, -7, 0, 2, 44, -3, -5, 66, -7];
$sample4 = [4, 25, 1, 6, 9, 5, 999, 4, 43, 2];

$sample2 = [];
for ($i = 0; $i &lt; mt_rand(10, 30); $i++) {
  $sample2[] = mt_rand(-1000, 1000);
}

extractNumbersThatSquaresAreGreaterThan($sample1);
extractNumbersThatSquaresAreGreaterThan($sample2);
extractNumbersThatSquaresAreGreaterThan($sample3);
extractNumbersThatSquaresAreGreaterThan($sample4);

var_dump($sample1);
//var_dump($sample2);
var_dump($sample3);
var_dump($sample4);

function extractNumbersThatSquaresAreGreaterThan(&amp;$list, $boundary = 26) {
  foreach ($list as $key =&gt; $value) {
    if (pow($value, 2) &lt;= $boundary) {
      unset($list[$key]);
    }
  }
}


Any hints for optimization? Is it possible, I've tried few, but in the end it was more complicated (and slower) solution
Hints?  Ok. 


Don’t square the values, O(n), instead square-root the boundary.  You’ll need an or in your test, though.
Don’t unset values in the list.  That requires shifting values in memory.  Make a new list of same size, fill in values, without gaps, and then resize result.