Reuben Gomes Reuben Gomes - 3 months ago 18
PHP Question

Randomize array


How Would I randomize this Array So that when i use a for each loop it would it would switch index 0 with key 1 and to randomize the order


iv tried $variable= array_rand($varialbe,count($variavle))
but it prints out

0 => int 0
1 => int 1
2 => int 2
3 => int 3
4 => int 4

foreach ($variable as $key)

array (size=10)
0 =>
object(stdClass)[25]
public 'id' => string '24' (length=2)
public 'course_name' => string 'Office Automation' (length=17)
public 'test_name' => string 'Test 2' (length=6)
public 'total_questions' => string '10' (length=2)
public 'duration' => string '20' (length=2)
public 'total_marks' => string '20' (length=2)
public 'question' => string 'Question1' (length=9)
public 'option1' => string 'ans1' (length=4)
public 'option2' => string 'ans2' (length=4)
public 'option3' => string 'ans3' (length=4)
public 'option4' => string 'ans4' (length=4)
public 'ans' => string 'D' (length=1)
public 'count' => string '1' (length=1)
1 =>
object(stdClass)[26]
public 'id' => string '25' (length=2)
public 'course_name' => string 'Office Automation' (length=17)
public 'test_name' => string 'Test 2' (length=6)
public 'total_questions' => string '10' (length=2)
public 'duration' => string '20' (length=2)
public 'total_marks' => string '20' (length=2)
public 'question' => string 'Question2' (length=9)
public 'option1' => string 'ans1' (length=4)
public 'option2' => string 'ans2' (length=4)
public 'option3' => string 'ans3' (length=4)
public 'option4' => string 'ans4' (length=4)
public 'ans' => string 'A' (length=1)
public 'count' => string '2' (length=1)
2 =>
object(stdClass)[27]
public 'id' => string '26' (length=2)
public 'course_name' => string 'Office Automation' (length=17)
public 'test_name' => string 'Test 2' (length=6)
public 'total_questions' => string '10' (length=2)
public 'duration' => string '20' (length=2)
public 'total_marks' => string '20' (length=2)
public 'question' => string 'Question3' (length=9)
public 'option1' => string 'ans1' (length=4)
public 'option2' => string 'ans2' (length=4)
public 'option3' => string 'ans3' (length=4)
public 'option4' => string 'ans4' (length=4)
public 'ans' => string 'B' (length=1)
public 'count' => string '3' (length=1)


How Would i do That

Need to just randomize the order so that when it prints it will print a new order everytime

The array stores question and question option in the keys/indexes

Answer

This function shuffles (randomizes the order of the elements in) an array. It uses a pseudo random number generator that is not suitable for cryptographic purposes.

shuffle for associative arrays, preserves key=>value pairs.

(Based on (Vladimir Kornea of typetango.com)'s function)

<?php
    function shuffle_assoc(&$array) {
        $keys = array_keys($array);

        shuffle($keys);

        foreach($keys as $key) {
            $new[$key] = $array[$key];
        }

        $array = $new;

        return true;
    }
?>

*note: as of PHP 5.2.10, array_rand's resulting array of keys is no longer shuffled, so we use array_keys + shuffle.

Comments