Re: Random array values from associative
| From: | Ernest E Vogelsinger | Date: | Tue, 18 Mar 2003 11:45:40 +0000 |
| Subject: | Re: Random array values from associative | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-140110@lists.php.net to get a copy of this message | ||
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/