dongliao1948 2014-10-21 23:26
浏览 32
已采纳

从数组中随机选择键以返回映射到该数组键的数据

Newbie here attempting to make a simple function, I think I'm close to a solution, but I'm stuck currently with an illegal offset type.

The function will, based on the number of keys, select one of the keys randomly and then propagate my echo statement so that the desired data will then propagate the function echo statements. i.e. if key '3' is selected, the Username, first name, and last name will be populated with the data associated with the third key '[3]' of the array '$aaUser'.

I have tried searching stack overflow, php.net and googled to try to find a solution to attempt to resolve this function but as yet I have not found an answer which I can understand at this time.

my example array:

$aaUser = [ //Make multidimensional
    // 0 => nameUser
    // 1 => namefirst
    // 2 => namelast
    0 => ["monkeework", "my", "name"],          
    1 => ["mentor", "Portia", "Plante"],                
    2 => ["teacher", "Bill", "Newman"],             
    3 => ["friend", "Sage", "Gerky"],                   
    4 => [ "pet", "pedro",  "" //last postion left empty - if empty we skip]            
    ];

my function:

function getRandomUser($arr){
    $myKey = array_rand($arr);
    $myValue = $arr[$myKey];

    echo 'Username: ' . $arr[$myValue][0] . '<br />'; 

    echo 'Name: ' . $arr[$myValue][1] . ' ' . $arr[$myValue][2] . '<br /><br />'; 

How I call the function:

    getRandomUser($aaUser);//select array, get array data back as echo statements
  • 写回答

1条回答 默认 最新

  • dsgdhf5674 2014-10-21 23:33
    关注

    You already set $myValue = $arr[$myKey]; so below where you echo the values you just need to reference $myValue[1] and not $arr[$myValue][1].

    function getRandomUser($arr){
        $myKey = array_rand($arr);
        $myValue = $arr[$myKey];
    
        echo 'Username: ' . $myValue[0] . '<br />'; 
    
        echo 'Name: ' . $myValue[1] . ' ' . $myValue[2] . '<br /><br />'; 
    

    $aaUser is passed into the function, and is now set as $arr inside the function. The first line returns a random key from $arr. The next line sets $myValue equal to the value of $arr[$myKey]. So for example.

    $myKey = 3; // randomly selected key
    $myValue = $arr[3] = $aaUser[3] = array("friend", "Sage", "Gerky");
    $myValue = array("friend", "Sage", "Gerky"); // what $myValue holds now
    

    $arr and $aaUser are the same. So whatever is held in $arr[3] will be set to $myValue. To access the array we just call $myValue[1].

    If you try to call $arr[$myValue][1] that's like saying $arr[array("friend", "Sage", "Gerky")][1].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥15 我的显卡支持CUDA最高版本是12.3,这个版本也支持VS 2022 17.0这种情况下如果我想下载CUDA11.8,需要下载旧版本的VS2022吗
  • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励