0

I always got a loop, when I run my code.

This is the code that I tried.

//ARRAY VALUES
$array = array();
$array[] = "201310656";
$array[] = "201310657";
$array[] = "201310658";
$array[] = "201310659";



function check ($array,$search){
    //IF FOUND
    if (in_array($search, $array)) {

        $search = intval($search);
                //+1 In Original Value If Found
        $search += 1;
        recheck($array,$search);

        return $unique_value;
    }
    //IF NOTFOUND RETURN ORIGINAL INPUT
    else{
        return $search;
    }

}
$unique_value = check($array,"201310659");

The results I wanted to attain is if the value exists recheck the array until output the unique value.

  • 1
    Shouldn't `recheck($array,$search);` be `$unique_value = check($array, $search);`? – Jonnix Jul 23 '19 at 17:39
  • 1
    @Jonnix `return check( $array, $search );` should suffice. – MonkeyZeus Jul 23 '19 at 17:41
  • Yeah, I know, but they already had `return $unique_value` so wanted to keep it as simple as possible :) – Jonnix Jul 23 '19 at 17:44
  • Thanks, @MonkeyZeus and It works. – スペースタイム Jul 23 '19 at 17:44
  • 1
    @Jonnix `$unique_value` is defined nowhere in the function so it's actually quite useless. – MonkeyZeus Jul 23 '19 at 17:45
  • @MonkeyZeus Thus I added the assignment so it would be defined... is it necessary, no, is it the simplest change to the code that would just work and not leave unreachable code? I'd argue yes. But sure, `return check($array, $search);` works just as well, just remember to remove the `return $unique_value;` afterwards. – Jonnix Jul 23 '19 at 17:47

0 Answers0