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 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计