V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
jingwentian
V2EX  ›  PHP

PHP数组问题

  •  
  •   jingwentian · 2013-11-22 11:19:59 +08:00 · 3313 次点击
    这是一个创建于 3801 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一个一维数组(包括 id ip time), 我想把ip重复5次及以上的数据拿出来, 求方法
    19 条回复    1970-01-01 08:00:00 +08:00
    lizheming
        2
    lizheming  
       2013-11-22 11:53:40 +08:00   ❤️ 1
    http://cn2.php.net/manual/zh/function.array-count-values.php
    你需要的应该是这个函数吧-_-!
    Actrace
        3
    Actrace  
       2013-11-22 13:13:22 +08:00
    是想要实现思路,还是先找方法?想找方法看楼上.
    tonitech
        4
    tonitech  
       2013-11-22 14:06:45 +08:00   ❤️ 1
    我会新开辟两个数组,一个用来计数,一个用来存超过5次的ip。
    计数的数组将ip作为数组的键,循环你的那个数组的时候计算这个key的ip出现的次数作为value存在数组里。
    如果超过五次就放进存放的数组里。
    jianghu52
        5
    jianghu52  
       2013-11-22 15:31:15 +08:00   ❤️ 1
    不会高深写法。只会最基础的东西。这个不能直接运行的。 你要先读明白代码。(没测试过,不过思想我觉得没错)
    $datalist = array{} //假设这里有一堆 ip,name,id
    $templist=array();//初始是空的。用来接内容
    foreach($datalist as $data){
    foreach($templist as $key=>$value){ //每次都来遍历这个数组,用ip找匹配
    if($data['ip']==$temp[$key]['ip']){//找到了就塞进去,然后+1
    $temp[$key]['count'] = $temp[$key]['count'] + 1;
    }else{//没找到就在temp里面新建一个ip
    $temp[]['ip']= $data['ip'];
    $temp[]['count']= 0;
    }
    }
    }
    //最后再遍历一遍temp
    $temp2=array();
    foreach($templist as $temp){
    if($temp['count']>5){
    $temp2[]=$temp;
    }
    }
    msg7086
        6
    msg7086  
       2013-11-22 16:01:01 +08:00   ❤️ 1
    嗯?一维数组?如果包含id/ip的话,至少是二维吧。

    先抽出ip放进数组,然后array_count_values一下,最后loop看count>5
    skydiver
        7
    skydiver  
       2013-11-22 16:18:27 +08:00   ❤️ 1
    @msg7086 不用loop,直接array_filter()就行了
    emric
        8
    emric  
       2013-11-22 17:19:09 +08:00   ❤️ 1
    http://gist.github.com/7597039
    想吐槽的已经被 @msg7086 说了...
    上面的都说的很赞! 如果连带数据, 我的思路大概是这样..
    wudikua
        9
    wudikua  
       2013-11-22 20:34:42 +08:00
    排序 然后输出,省空间。如果你这IP ID来自日志 直接用linux命令来的更快,sort然后再uniq
    tonitech
        10
    tonitech  
       2013-11-22 22:27:00 +08:00
    @jianghu52 按照楼主的需求,大于5就行了,所以你的计数器和最后的遍历是可以放在一起的,这样少循环一次。
    tonitech
        11
    tonitech  
       2013-11-22 22:39:55 +08:00
    @jianghu52 纸上谈兵没用,我来把你的改写一段吧。。。

    $datalist = array{} //假设这里有一堆 ip,name,id
    $templist = array();//初始是空的。作为一个key=>value的数据结构方便查找
    $result = array();//结果

    foreach ($datalist as $data) {
    $templist[$data['ip']][] = $data;
    }
    //最后再遍历一遍templist
    foreach ($templist as $ip => $temp) {
    if (count($temp) >= 5) {
    $result[$ip] = $temp;
    }
    }
    bombless
        12
    bombless  
       2013-11-22 23:27:41 +08:00
    <?php
    function myfilter($src, $limit = 5){
    $ref = array();
    //先建立一个数组$ref,维护起一个ip到项的映射,以及ip出现的次数
    foreach($src as $key => $item){
    if (isset($ref[$item['ip']])){
    $ref[$item['ip']]['count'] += 1;
    $ref[$item['ip']]['keys'][] = $key;
    }else{
    $ref[$item['ip']] = array('count' => 1, 'keys' => array($key));
    }
    }
    //现在遍历所有的ip,并将出现次数达到$limit的项插入到用于返回的$ret数组中
    $ret = array();
    foreach($ref as $list){
    if ($list['count'] >= $limit){
    foreach($list['keys'] as $key){
    $ret[] = $src[$key];
    }
    }
    }
    return $ret;
    }
    bombless
        13
    bombless  
       2013-11-22 23:29:18 +08:00
    https://gist.github.com/anonymous/7601723

    丧心病狂,v2ex竟把我的空格给吃掉了
    bombless
        14
    bombless  
       2013-11-22 23:40:47 +08:00
    https://gist.github.com/anonymous/7601890
    发觉还可以简化。但总觉得虽然代码短了但反而丧失了一点可读性。
    bombless
        15
    bombless  
       2013-11-22 23:44:30 +08:00   ❤️ 1
    https://gist.github.com/anonymous/7601954
    加了7个字符,不知道可读性是不是稍微好一点了^_^
    msg7086
        16
    msg7086  
       2013-11-23 09:57:08 +08:00   ❤️ 2
    https://gist.github.com/msg7086/7609816

    我觉得这样简单些 @bombless

    话说,题主去哪了……
    bombless
        17
    bombless  
       2013-11-23 10:25:25 +08:00   ❤️ 1
    @msg7086
    的确~

    个人来说还是下意识的写PHP 5.2兼容的代码,哈哈。

    楼主一定是周末不上班,所以把问题留到周一解决>_<

    话说大清早的gist就墙了……
    我还是改了下网址在//gist.github.com/msg7086/7609816才看到你的代码
    jingwentian
        18
    jingwentian  
    OP
       2013-11-23 23:44:13 +08:00
    @msg7086
    @bombless
    哈哈...周末不上班都被你们发现了
    很感谢你们 (已感谢^_^)
    那个...周五的时候写了个姑且能用
    https://gist.github.com/JingwenTian/7615903
    jingwentian
        19
    jingwentian  
    OP
       2013-11-23 23:48:20 +08:00
    @bombless V2上贴Gist代码不是直接贴页面URL么
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   901 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 21:17 · PVG 05:17 · LAX 14:17 · JFK 17:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.