php 从集合中选择 X 个项目?
我正在使用代码点火器从数据库检索数据
返回的是一个数组对象
,就像
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 的变量,它包含任意数字(尽管总是小于数组中的对象数量)。
我的问题是:
说 count = 3
, 我如何从对象中获取 3 个随机 id 的数组?
像 array(4, 9, 1)
我只想获取一次单个 id,
因此 array(4, 4, 9)
是不正确的。
请注意,id 不是线性的。
I am using code igniter to retrieve data from a database
What is returned is an array objects
something like
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
}
)
I have a variable called count, this contains an arbitrary number (although always less than the number of objects in the array).
My question is:
Say count = 3
,
How would I get an array of 3 random id's from the object?
something like array(4, 9, 1)
I only want to get a single id once
so array(4, 4, 9)
would be incorrect.
Note that the id's are not linear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
array_rand
解释 -
array_rand
explain -