How can I randomize an array

Randomly arrange the elements in an array (Theme: PHP examples)

The function is used to arrange the elements in an array at random shuffle (& $ array).
PHP code<?php $arr = array('eins', 'zwei', 'drei', 'vier', 'fünf'); shuffle($arr); // das Array zufällig sortieren echo(implode(', ', $arr)); // Werte, getrennt durch Kommata ausgeben ?>
outputthree, five, one, four, two

If you want to implement your own function for random arrangement, it could look like this (whereby mt_rand () is used to generate random numbers):
PHP code<?>
outputfour, one, five, three, two

In order to sort a multidimensional array randomly, a function can be implemented which is called recursively:
PHP code<?php function shuffle_recursive($arr) { shuffle($arr); foreach ($arr as $key=>$val) { if (is_array($val)) { shuffle_recursive($val); } } } $arr = array( array('eins', 'zwei', 'drei'), array('zehn', 'zwölf', 'dreizehn') ); shuffle_recursive($arr); var_dump($arr); ?>
outputarray (2) {[0] => array (3) {[0] => string (8) "thirteen" [1] => string (4) "ten" [2] => string (5) "twelve "} [1] => array (3) {[0] => string (4)" one "[1] => string (4)" three "[2] => string (4)" two "}}