So, I build a little php array looping. The objective is : the loop will finish when one of array value is empty.
This is the code:
<?php
if(isset($_POST['submit'])) {
$Var_1 = array('Manggo_1' => rand(1, 3),
'Manggo_2' => rand(1, 3),
'Manggo_3' => rand(1, 3),
'Manggo_4' => rand(1, 3),
'Manggo_5' => rand(1, 3),
'Manggo_6' => rand(1, 3)
);
$Var_2 = array('Manggo_7' => rand(1, 3),
'Manggo_8' => rand(1, 3),
'Manggo_9' => rand(1, 3),
'Manggo_10' => rand(1, 3),
'Manggo_11' => rand(1, 3),
'Manggo_12' => rand(1, 3)
);
while (!(empty($Var_1) && empty($Var_2))) {
foreach ($Var_1 as $value) {
echo "$value, ";
if ($value == 3) {
unset($value);
} elseif ($value == 1) {
array_push($Var2, $value);
}
}
foreach ($Var_2 as $value) {
echo "$value, ";
if ($value == 3) {
unset($value);
} elseif ($value == 1) {
array_push($Var1, $value);
}
}
}
}
So, that's all my php code, If the $value == 3, I want to destroy the value, and if == 1, I want to insert the value to another array. Loop until one of array is empty.
The question is: How to print/echo the result from each loop iteration (after click the submit button), until one of the array value is empty ?? I always get looping forever.
Thanks.
foreach ($Var_1 as &$value), now you can unset the actual array-item! have a look here: php.net/manual/en/control-structures.foreach.php