note 80223 added to function.array-rand

From: Date: Fri, 04 Jan 2008 18:57:54 +0000
Subject: note 80223 added to function.array-rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-134779@lists.php.net to get a copy of this message
Here is a function to pick a random fixed-size subset of an array without changing the order. It's possible to be slightly faster in some cases if you know the array keys are numeric or if you're only picking a few elements, but this is a good general implementation. <?php /** * Pick $num elements from $array at random, preserving order and keys. */ function array_pick($hash, $num) { $count = count($hash); if ($num <= 0) return array(); if ($num >= $count) return $hash; $keys = array_rand($hash, $count - $num); foreach ($keys as $k) unset($hash[$k]); return $hash; } ?> ---- Server IP: 209.41.74.194 Probable Submitter: 66.90.244.29 ---- Manual Page -- http://www.php.net/manual/en/function.array-rand.php Edit -- https://master.php.net/note/edit/80223 Del: integrated -- https://master.php.net/note/delete/80223/integrated Del: useless -- https://master.php.net/note/delete/80223/useless Del: bad code -- https://master.php.net/note/delete/80223/bad+code Del: spam -- https://master.php.net/note/delete/80223/spam Del: non-english -- https://master.php.net/note/delete/80223/non-english Del: in docs -- https://master.php.net/note/delete/80223/in+docs Del: other reasons-- https://master.php.net/note/delete/80223 Reject -- https://master.php.net/note/reject/80223 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#134779) next »