programming-examples/php/Arrays/PHP function to shuffle an associative array, preserving key, value pairs..php

20 lines
382 B
PHP
Raw Normal View History

2019-11-15 12:59:38 +01:00
<?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));
?>