Marco.org

I’m : a programmer, writer, podcaster, geek, and coffee enthusiast.

I hate PHP’s array_rand() function

…so I made a handy util function to do what I really want:

// Does what array_rand() should do: 
//  returns the VALUES instead of the keys.
function array_return_rand($input, $num_req = 1)
{
    $keys = array_rand($input, $num_req);
    if ($num_req == 1) {
        return $input[$keys];
    } else {
        $out_array = array();
        foreach ($keys as $k) $out_array[$k] = $input[$k];
        return $out_array;
    }
}