Answer the question
In order to leave comments, you need to log in
How to repeat a loop in php?
Task: if the last element of the array == 9 then delete it
The code works:
$run = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9);
$count_run = count($run);
$last = $count_run-1;
for ($z = 0; $z < $count_run; $z++) {
if($z == $last && $run[$z] == 9){
unset($run[$z]);
sort($run);
}
}
Answer the question
In order to leave comments, you need to log in
$run = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9);
foreach($run as $key=>$val)
if($val==9)
unset($run[$key]);
sort($run);
You need to place your cycle in another cycle. This loop should only terminate if the last element has not been removed. Or if there is no more data in the array. Recursion is completely unnecessary here :-)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question