I have an array of objects in PHP. I need to randomly select 8 of them. My initial thought was to use array_rand(array_flip($my_array), 8), but this doesn't work because objects cannot be keys to arrays.
I know I could use shuffle, but I'm worried about performance getting worse as the array grows. Is this the best way, or is there a more efficient way?
Note that the
shuffle()function passes arguments as references and changes them.$result = array(); foreach( array_rand($my_array, 8) as $k ) { $result[] = $my_array[$k]; }