dongqian1893 2013-01-16 16:43
浏览 40
已采纳

php数组循环用数组循环来查找值

I have a two-dimensional array $array_userinfo with approx. 21,000 elements. Each element has 'login' in first position and 'email' in the second position. What I need to do is to loop through this array and find any matching 'login' from another $array_usermeetingfolder, which is a one dimensional array with approx. 700 elements.

Here are the two arrays populated:

foreach ($child->children() as $node_principalid) {
array_userinfo[$i][0] = (string) $node_principalid->login;
$array_userinfo[$i][1] = (string) $node_principalid->email;
i++;                            
}

foreach (.......) {
$endusersconame =  (string) $usermeetingsfoldersco->children()->name;       
$array_usermeetingfolder[$j] = $endusersconame;//
 $j++;
}

What is the best/efficient way way to do this? I have a loop like this:

for ($k=0;$k < count($array_userinfo);$k++) {
//tempecho($array_userinfo[$k][0]);//will yield login, such as, 'joeblow'
}

Thanks.

  • 写回答

3条回答 默认 最新

  • dtvgo28624 2013-01-16 17:18
    关注

    if it is possible you should use the login as an array key, then you can fast access the key and I think you don't need to think about performance. Arrays in PHP are Hashmaps and if you know the key you can access it fast if you have to check the whole array it takes a lot longer. If you can not change it, you should think about creating a data structure from the array to habe this performance increase. Don't know wether it's worth, measure it :)

    like:

    <?php
    foreach($array_userinfo as $info) {
        $checkAgainst[$info[0]] = $info[1];
    }
    
    foreach($array_usermeetingfolder as $mail) {
        if(isset($checkAgainst[$mail]) {
            $return[$mail] = $checkAgainst[$mail];
        }
    }
    
    // use $return
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应