dourong6054 2018-04-19 12:32
浏览 77
已采纳

使用array_rand从合并数组中获取单个数字

I want to get a random select from an array, but it does show other number that is not from my selected arrays.

<?php
$MainArray = array(
    'IC' => array(4,19,22,42,61,80,82,88),
    'IR' => array(9,12,17,45,46,67,68,89),
    'JP' => array(6,26,39,53,93),
    'DP' => array(1,8,14,35,38,59,70,71),
    'TA' => array(0,2,3,5,7,10,11,13,15,16,18,20,21,23,24,25,27,28,29,30,31,32,33,34,36,37,40,41,43,44,47,48,49,50,51,52,54,55,56,57,58,60,62,63,64,64,66,69,72,73,74,75,76,77,78,79,81,83,84,85,86,87,90,91,92,94,95,96,97,98,99)
);
$ArrayNumbers = array_merge($MainArray['IC'], $MainArray['IR'], $MainArray['TA'], $MainArray['DP']);
$setSelectedNumber = array_rand($ArrayNumbers);
if(in_array($setSelectedNumber, $MainArray['JP'])){
    echo 'ERROR FOUND: '.$setSelectedNumber;
}else{
    echo $setSelectedNumber;
}
?>

Am hoping to get a randomly match number from the merged arrays, but it also shows number from the JP array 6,26,39,53,93 which I don't want.

  • 写回答

2条回答 默认 最新

  • dousuo8400 2018-04-19 12:36
    关注

    array_rand returns a randomly picked key rather than value. You can get the value by this key as follows:

    $setSelectedNumber = $ArrayNumbers[array_rand($ArrayNumbers)];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀