Small array problem

Source: Internet
Author: User
Small array problem
 array('id' => '11'),'1'=>array('id' => '22'),);$rand_keys = array_rand($roomlist, 1);var_dump($rand_keys);echo gettype($rand_keys);echo ("\n");echo ("\n");$rand_keys = array_rand($roomlist, 2);var_dump($rand_keys);echo gettype($rand_keys);?>


Output:
Int (0)
Integer

Array (2 ){
[0] =>
Int (0)
[1] =>
Int (1)
}
Array

A small problem. for the first test, how does one return an integer instead of an array?


Reply to discussion (solution)

The array_rand () function returns the random key name in the array, or if you specify that the function returns more than one key name, the array containing the random key name is returned.

It seems that only the key is returned.

The array_rand () function returns the random key name in the array, or if you specify that the function returns more than one key name, the array containing the random key name is returned.



I wonder why when the second parameter of array_rand is set to 1, the returned result is not an array, but an integer.

If I use this method, a bug will occur.
 Array ('id' => '11'),); $ array_size = count ($ roomlist) $ rand_keys = array_rand ($ roomlist, $ array_size ); // when the size is 1 and the return value is integer, size> = 2, the return value is array. the following operations cannot unify var_dump ($ rand_keys); echo gettype ($ rand_keys ); echo ("\ n"); echo ("\ n"); $ val = $ roomlist [$ rand_keys [0];


Why ?? Because $ rand_keys is integer, a bug exists.

It seems that only the key is returned.



Only the key is returned. in c ++, the returned array is the array, and the returned integer is the integer, which is not changed because of the changes in the function parameters ..

Otherwise, special processing is required .. It is also unreasonable. one function can return two types... View my tests

Definition and usage
The array_rand () function returns the random key name in the array, or if you specify that the function returns more than one key name, the array containing the random key name is returned.
Description
The array_rand () function randomly selects one or more elements from the array and returns them.
The second parameter is used to determine the elements to be selected. If more than one element is selected, an array containing the random key name is returned. Otherwise, the key name of the element is returned.

If you do not understand the manual, or you have to question the description in the manual. Then the gods cannot help you.

The manual has been clearly written:
------
Return value:
If you only retrieve one key, array_rand () returns the key name of a random unit. Otherwise, an array containing the random key name is returned. In this way, you can randomly retrieve the key name and value from the array.

The bug is indeed a bug ..

The scripting language is amazing ..... La la

C ++ er doesn't understand la

In addition, the answer from the upstairs doesn't tell me that I am a newbie. if I find the correct/good explanation, I won't post it either ..........



I found the official website statement.

Http://php.net/manual/zh/function.array-rand.php

I will meet new users like me later. this is the official php Manual.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.