本文介绍了PHP从集合X的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用code点火检索数据库中的数据
什么是返回是一个数组对象
像
阵列(
[0] = {
手机=> 027xxxxxx,
ID => 1
},
[1] = {
手机=> 027xxxxxx,
ID => 4
},
[2] = {
手机=> 027xxxxxx,
ID =>五
},
[3] = {
手机=> 027xxxxxx,
ID => 7
},
[4] = {
手机=> 027xxxxxx,
ID => 9
},
[5] = {
手机=> 027xxxxxx,
ID => 10
},
[6] = {
手机=> 027xxxxxx,
ID => 112
},
[7] = {
手机=> 027xxxxxx,
ID => 113
}
)
我有一个叫做计数变量,这包含任意数目(尽管总是低于物体的阵列中的数更少)。
我的问题是:
说计数= 3
,
我该如何获得的3个随机ID的数组从对象?
像阵列(4,9,1)
我只是想获得一个ID一次
所以阵列(4,4,9)
是不正确的。
请注意,该ID是不是线性的。
解决方案
$ random_keys = array_rand(array_keys($ your_array),3);
解释 -
- array_keys只返回原始数组 关键
- array_rand会再挑不随机重复
- 和阵列的回报是包含关键是你的原数组
- 让你可以从原始数组的任何信息
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.
解决方案
$random_keys = array_rand(array_keys($your_array), 3);
explain -
- array_keys only return the key from the original array
- array_rand will then pick randomly without repeatability
- and the return of array is contains key to your original array
- so you can retrieve any information from original array
这篇关于PHP从集合X的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!