Change Array Values Randomly - Php

random array

Snippet Code


  
Rate this page :
  [ 0 votes]

Using this we can randomly change array values

<?php function arrayToRandom($array){ $array = (!is_array($array)) ? array($array) : $array; $a = array(); $max = count($array) 10; while(count($array) > 0){ $e = array_shift($array); $r = rand(0, $max); while (isset($a[$r])){ $r = rand(0, $max); } $a[$r] = $e; } ksort($a); $a = array_values($a); return $a; } $colorarr=Array("Red","Green","Blue","Orange","Yellow");//array of colors $res = arrayToRandom($colorarr); print_r($res); ?>

Tags


Ask Questions

Ask Question