note 79147 added to function.array

From: Date: Tue, 13 Nov 2007 18:46:42 +0000
Subject: note 79147 added to function.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-132231@lists.php.net to get a copy of this message
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) ; } ---- Server IP: 69.147.83.197 Probable Submitter: 65.207.49.92 ---- Manual Page -- http://www.php.net/manual/en/function.array.php Edit -- https://master.php.net/note/edit/79147 Del: integrated -- https://master.php.net/note/delete/79147/integrated Del: useless -- https://master.php.net/note/delete/79147/useless Del: bad code -- https://master.php.net/note/delete/79147/bad+code Del: spam -- https://master.php.net/note/delete/79147/spam Del: non-english -- https://master.php.net/note/delete/79147/non-english Del: in docs -- https://master.php.net/note/delete/79147/in+docs Del: other reasons-- https://master.php.net/note/delete/79147 Reject -- https://master.php.net/note/reject/79147 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132231) next »