0

it says an error undefined offset i dont know what causes it

im trying to shuffle $numberarray without repeating the number

heres the code

$numberarray = array(1,2,3,4,5,6,7,8,9,10);
for($counter=0;$counter<=9;$counter++)
        {
        $b = $counter - 1;
        $a = $numberarray[$counter];
            $numberarray[$counter] = rand(1,10);
            do
            {
                $numberarray[$counter] = rand(1,10);

                while($b != 0)
                {
                    if($numberarray[$counter] == $numberarray[$b])
                    {

                      $numberarray[$counter] = rand(1,10);
                      $b = $counter - 1;
                      //echo $b;
                    }
                    else
                    {
                      $b--;
                    }
            }

    }while($a == $numberarray[$counter]);
    echo $numberarray[$counter].", ";
}

sample output $numberarray = {3,4,5,1,2,7,9,10,8,5}

2
  • why not use shuffle function inbuilt in php details at php.net/manual/en/function.shuffle.php Commented Sep 27, 2014 at 16:03
  • Because $b being negative int value. Commented Sep 27, 2014 at 16:05

2 Answers 2

1

This would be better:

(By the way you don't need an extra condition before the while since while act like a condition itself)

     $numberarray = array(1,2,3,4,5,6,7,8,9,10);
     for($counter=0;$counter<=9;$counter++)
        {
        $b = $counter - 1;
        $a = $numberarray[$counter];
            $numberarray[$counter] = rand(1,10);
            do
            {
              $numberarray[$counter] = rand(1,10);

             while($b >0)
             {
                if($numberarray[$counter] == $numberarray[$b])
                {

                    $numberarray[$counter] = rand(1,10);
                    $b = $counter - 1;
                    //echo $b;
                }
                else
                {
                    $b--;
                }
             }

        }while($a == $numberarray[$counter]);
        echo $numberarray[$counter].", ";
    }

Even if there is no more error, your code repeat numbers sometimes, so I would symply do it like this using shuffle:

      $numberarray2 = array(1,2,3,4,5,6,7,8,9,10);
      shuffle($numberarray2);
      print_r($numberarray2);
Sign up to request clarification or add additional context in comments.

1 Comment

tnx for your answer sir but its not working sir... its still repeating other numbers i like to shuffle them by no repeating numbers sample output $numberarray = {3,4,1,2,7,5,6,10,8,9}
0

The problem is that in the first $b is equal to -1 that's why you get the error so i think you should delete the if condition and edit the while statement to while($b!=-1).

3 Comments

gosh, where did you get this idea?!
@AdamSinclair As it was posted at about the same time as yours, probably from reading the question and thinking about it. Great minds sometimes think alike - doesn't necessarily mean you were copied from.
@AdamSinclair Magic isn't it beautiful.Also My mind can't think that's why I use sorcery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.