Write a PHP function to shuffle an associative array, preserving key, value pairs
<?php function shuffle_assoc($my_array) { $keys = array_keys($my_array); shuffle($keys); foreach($keys as $key) { $new[$key] = $my_array[$key]; } $my_array = $new; return $my_array; } $colors = array("color1"=>"Red", "color2"=>"Green", "color3"=>"Yellow"); print_r(shuffle_assoc($colors)); ?>
Sample Output:
Array ( [color1] => Red [color2] => Green [color3] => Yellow )
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
Array ( [color1] => Red [color2] => Green [color3] => Yellow )need an explanation for this answer? contact us directly to get an explanation for this answer