Shuffle a associated array. remain association between keys and values

Views: 26 Last modified: September 17th, 2011 Comments: 0

This snippet will accept 1 parameter which should be an array. It takes the array, shuffles the array and returns a shuffled array. The new shuffled array will remain the association between keys and values!

Returns array if the input was an array else it will return false!

Use:

$shuffled = shuffle_array($arr);
function shuffle_array($arr)
{
	if (is_array($arr))
	{
		$temp = array();
		$final = array();

		foreach ($arr as $key => $value)
		{
			$temp[][$key] = $value;
		}

		shuffle($temp);

		foreach ($temp as $both)
		{
            while(list($a,$b) = each($both))
			{
				$final[$a] = $b;
			}
		}
			return $final;
		}
		else
		{
			return false;
		}
}
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)
    Bluehost

    Mail this!

    To: From:Sum {0+5} =  
    Anything to add ?

        You must be logged in to post a comment.