我试图在getResult()之后使用array_rand()进行随机实体选择。 我希望得到一个随机实体,但得到int 1(int 2,int 3等)。为什么呢?
$qb2->select('u')
->from('Application\Entity\User', 'u')
->where('u.usrlId = :sale');
if( !empty($usersIdListForExcluding) ) {
$qb2->andWhere($qb2->expr()->notIn( 'u.usrId', $usersIdListForExcluding ));
}
$qb2->setParameter('sale', 2);
$salesSet = $qb2
->getQuery()->getArrayResult();
var_dump(array_rand($salesSet));
var_dump($salesSet);
首先var_dump给出:
int 1
第二个:
array (size=3)
0 =>
object(DoctrineORMModule\Proxy\__CG__\Application\Entity\User)[552]
public '__initializer__' =>
object(Closure)[528]
public '__cloner__' =>
object(Closure)[529]
public '__isInitialized__' => boolean true
private 'usrId' (Application\Entity\User) => int 2
private 'usrName' (Application\Entity\User) => string 'sales_test_1' (length=12)
private 'usrPassword' (Application\Entity\User) => string 'pass' (length=4)
private 'usrEmail' (Application\Entity\User) => string 's_1@gmail.com' (length=13)
...
1 =>
object(Application\Entity\User)[567]
private 'usrId' => int 6
private 'usrName' => string 'sales_test_3' (length=12)
private 'usrPassword' => string 'pass' (length=4)
private 'usrEmail' => string 's_3@gmail.com' (length=13)
private 'usrlId' => int 2
...
2 =>
object(Application\Entity\User)[526]
private 'usrId' => int 7
private 'usrName' => string 'sales_test_4' (length=12)
private 'usrPassword' => string 'pass' (length=4)
private 'usrEmail' => string 's_4@gmail.com' (length=13)
...
答案 0 :(得分:0)
array_rand
返回一个随机密钥(而不是随机值)。如果您想要该值,则可以通过密钥访问它。
$random_key = array_rand($salesSet);
$random_value = $salesSet[$randomKey];