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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵