Re: Random array values from associative

From: Date: Tue, 18 Mar 2003 11:48:30 +0000
Subject: Re: Random array values from associative
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-140111@lists.php.net to get a copy of this message
Thanks for your reply, I now have only the keys but I need both keys and the value. ?? any suggestions? ----- Original Message ----- From: "Ernest E Vogelsinger" <ernest@vogelsinger.at> To: "Awlad Hussain" <php@sheridan-phoenix.com> Cc: <php-general@lists.php.net> Sent: Tuesday, March 18, 2003 11:45 AM Subject: Re: [PHP] Random array values from associative > At 12:40 18.03.2003, Awlad Hussain said: > --------------------[snip]-------------------- > >I have an associative like array["name"]="hello"; > >I am trying to get two random values from the array, the key name and the > >value, but using the codes below from the PHP manual i am getting the inex > >key value rather than the name. > > > >$rand_keys = array_rand ($input, 2); > >print $input[$rand_keys[0]]."\n"; > >print $input[$rand_keys[1]]."\n"; > > > >so instead of getting two random values like key=name & value="hello" i am > >getting key=0 and value=hello. > --------------------[snip]-------------------- > > array_rand only operates on the "value side" of an array. To retrieve > random keys from an associative array, use array_keys() of the assoc. array > as input rather that the assoc. array itself: > > $rand_keys = array_rand (array_keys($input), 2); > print $input[$rand_keys[0]]."\n"; > print $input[$rand_keys[1]]."\n"; > > > > -- > >O Ernest E. Vogelsinger > (\) ICQ #13394035 > ^ http://www.vogelsinger.at/ > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#140111) next »