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 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据