|
php restart function inside function
hi all, im really confused how to do the following task, basically im cheking for a value example:
function counter1
if($counter == 1){
than start function counter2();
}
function counter2(){
start function counter1();
}
//The above is the princible. the below code is an example of the code im working on where i have indicated where to call the function i just dont know how to.
<?php
$checker = 0;
function shuffleArray($myArray) {
$value_count = array_count_values($myArray);
$last_value = $myArray[count($myArray) - 1];
unset($myArray[count($myArray) - 1]);
$shuffle = array();
$last = false;
while (count($myArray) > 0) {
$keys = array_keys($myArray);
$i = round(rand(0, count($keys) - 1));
while ($last === $myArray[$keys[$i]] ) {
$i = round(rand(0, count($keys) - 1));
echo " stuck ";$checker++;
if($checker>10){
echo " Too many checks so stop, here i should restart ";
Here is where i want to call a function bob()
}
}
$shuffle[] = $myArray[$keys[$i]];
$last = $myArray[$keys[$i]];
unset($myArray[$keys[$i]]);
}
if ($last_value === $last) {
$i = 0;
foreach($shuffle as $key=>$value) {
if ($value !== $last_value) {
$i = $key;
break;
}
}
array_slice($shuffle, $i + 1, 0, $last_value);
} else {
$shuffle[] = $last_value;
}
return $shuffle;
}
print_r(shuffleArray(array(1,5,5,3,7,7,7,7)));
function bob(){
while($checker>10){
echo "checker is at 10";
here i want to call the function "shuffleArray($myArray) "
}
}
?>
the output will shuffle this array (1,5,5,3,7,7,7,7)except the numbers will not be next to each other for example 1,7,5,7,3,7,5,7 if this results fails than it will process again until the result is acceptable, there are many outcomes that could output" all outputs are exceptable if the numberes are not together but sometimes the random shuffle may create the following 1,5,3,7,5,7 and allthat is left is 7's wich is not acceptable so when this happens i want to rerun the script.
Last edited by parele; 03-07-2012 at 07:33 AM..
|