I tried to unset all records smaller than 60, but every time only a limited number were unset. Here is my code:
echo "<p>============= count=".count($ar); // 1764
for ($i=0;$i<count($ar);++$i)
if ((strlen($ar[$i])<60) OR (strpos($ar[$i],'src=')<5))
unset ($ar[$i]);
$ar=array_values($ar); // renumber the indices
echo "<p>============================= count=".count($ar);
// 1452; OK, so they are all larger than 60 chars, right?
for ($i=0;$i<count($ar);++$i)
if (strlen($ar[$i])<60)
echo"<br>$i - ".$ar[$i]; // not so; several items printed!!! ???
for ($i=0;$i<count($ar);++$i)
if (strlen($ar[$i])<60)
unset ($ar[$i]); //again
$ar=array_values($ar);
echo "<p>============================= count=".count($ar); // 1396; OK, now all are larger than 60 chars?
for ($i=0;$i<count($ar);++$i)
if (strlen($ar[$i])<60)
echo"<br>$i - ".$ar[$i]; // no, still items printed!!! ???
for ($i=0;$i<count($ar);++$i)
if (strlen($ar[$i])<60)
unset ($ar[$i]);
$ar=array_values($ar);
echo "<p>============================= count=".count($ar); // 1386; so 10 more were removed; are we done now?
for ($i=0;$i<count($ar);++$i)
if (strlen($ar[$i])<60)
echo"<br>$i - ".$ar[$i]; // again still items printed!!! ???
How is it possible that after every loop and removal of items less than 60 chars, there are still array-items not unset that will be unset at a next repeat? I am really at my wits end. What did I do wrong? Can somebody explain, please. Thanks....Eke
Use a
foreachloop - https://www.php.net/manual/en/control-structures.foreach.phpThe issue is that the number of elements in the array is changing on the loop iterations when items are being removed.
A better approach is to use
array_filter- https://www.php.net/manual/en/function.array-filter.php