note 79147 deleted from function.array by sobak

From: Date: Fri, 25 Nov 2016 07:37:35 +0000
Subject: note 79147 deleted from function.array by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207669@lists.php.net to get a copy of this message
Note Submitter: sam barrow ---- check out these functions - retrieves a random element or key of an array (numeric or string indexes, doesn't matter). function randomKey(array $array) { global $mod, $plg, $sec ; $rand = rand(0, count($array) - 1) ; $counter = 0 ; foreach (array_keys($array) as $key) { if ($counter++ == $rand) { return $key ; } } trigger_error('Array is empty.', E_USER_WARNING) ; } function randomElement(array $array) { global $mod, $plg, $sec ; $rand = rand(0, count($array) - 1) ; $counter = 0 ; foreach ($array as $value) { if ($counter++ == $rand) { return $value ; } } trigger_error('Array is empty.', E_USER_WARNING) ; }

« previous php.notes (#207669) next »