php从集合中选择X项?
我使用CodeIgniter从数据库中检索数据 返回的是一个数组对象 差不多php从集合中选择X项?,php,arrays,random,object,Php,Arrays,Random,Object,我使用CodeIgniter从数据库中检索数据 返回的是一个数组对象 差不多 array( [0] = { mobile => '027xxxxxx', id => 1 }, [1] = { mobile => '027xxxxxx', id => 4 }, [2] = { mobile => '027xxxxxx', id => 5 }, [3] = {
array(
[0] = {
mobile => '027xxxxxx',
id => 1
},
[1] = {
mobile => '027xxxxxx',
id => 4
},
[2] = {
mobile => '027xxxxxx',
id => 5
},
[3] = {
mobile => '027xxxxxx',
id => 7
},
[4] = {
mobile => '027xxxxxx',
id => 9
},
[5] = {
mobile => '027xxxxxx',
id => 10
},
[6] = {
mobile => '027xxxxxx',
id => 112
},
[7] = {
mobile => '027xxxxxx',
id => 113
}
)
我有一个名为count的变量,它包含一个任意数(尽管总是小于数组中的对象数)
我的问题是:
说计数=3
,
如何从对象中获取3个随机id的数组
类似于数组(4,9,1)
我只想得到一个身份证一次
所以数组(4,4,9)
是不正确的
请注意,id不是线性的
$random_keys = array_rand(array_keys($your_array), 3);
说明-
- 数组\密钥仅返回原始数组中的密钥
- 然后,阵列随机选取,无重复性
- 数组的返回是原始数组的包含键
- 所以您可以从原始数组中检索任何信息
- 数组\密钥仅返回原始数组中的密钥
- 然后,阵列随机选取,无重复性
- 数组的返回是原始数组的包含键
- 所以您可以从原始数组中检索任何信息