php页面中,有个二维数组$arr_c,有三个属性:用户id,进的页面url,进的时间,现打印出来的结果出下:
Array ( 
[0] => Array ( [userId] => 1[thisUrl] => a.php [dateTime] => 2010-11-17 14:48:30 ) 
[1] => Array ( [userId] => 1[thisUrl] => b.php [dateTime] => 2010-11-17 14:42:57 ) 
[2] => Array ( [userId] => 2[thisUrl] => c.php [dateTime] => 2010-11-17 15:02:25 ) 
[3] => Array ( [userId] => 2[thisUrl] => c.php [dateTime] => 2010-11-17 18:37:05 ) 
[4] => Array ( [userId] => 5[thisUrl] => f.php [dateTime] => 2010-11-17 14:37:05 ) 
[5] => Array ( [userId] => 4[thisUrl] => e.php [dateTime] => 2010-11-18 15:02:25 ) 

现要求:返回一个二维数组:同一天只进了一页的用户量 (数组中含日期 人数)
应该要得到下面的数组
Array ( 
[0] => Array ( [num] => 3 [dateTime] => 2010-11-17 ) 
[1] => Array ( [num] => 1 [dateTime] => 2010-11-18 ) 

这应该如何实现,thanks

解决方案 »

  1.   

    一个foreach循环就搞定了啊··
    foreach($arr as $k=>$v){
       //将$v截取出来日期
       将日期作为key重组一下数组 如果存在 num+1
    }
      

  2.   

    你返回的数据和你需求不符啊,如果是同一天只进了一页的用户量,
    数组应该为
    Array ( 
    [0] => Array ( [num] => 2 [dateTime] => 2010-11-17 ) 
    [1] => Array ( [num] => 1 [dateTime] => 2010-11-18 ) 
    )  
      

  3.   

    哦,对,我多算了一个用户,
    11-17 应该是二个,
    这如何实现 呀thanks
      

  4.   

    我刚才仔细想了一下,事情没这么简单~我将我的实现代码发上来,欢迎拍砖<?php
    echo "<pre>";
    $arr = array (  
    '0' => array ( 'userId' => 1,'thisUrl' => 'a.php', 'dateTime' => '2010-11-17 14:48:30' ),  
    '1' => array ( 'userId' => 1,'thisUrl' => 'b.php', 'dateTime' => '2010-11-17 14:42:57' ), 
    '2' => array ( 'userId' => 2,'thisUrl' => 'c.php', 'dateTime' => '2010-11-17 15:02:25' ),  
    '3' => array ( 'userId' => 2,'thisUrl' => 'c.php', 'dateTime' => '2010-11-17 18:37:05' ),  
    '4' => array ( 'userId' => 5,'thisUrl' => 'f.php', 'dateTime' => '2010-11-17 14:37:05' ),  
    '5' => array ( 'userId' => 4,'thisUrl' => 'e.php', 'dateTime' => '2010-11-18 15:02:25')  
    );//print_r($arr);foreach($arr as $k=>$v){
    $v['dateTime'] = substr($v['dateTime'],0,10);
    $new_arr[$v['dateTime']][$v['userId']][$v['thisUrl']] = $v;
    }foreach($new_arr as $d=>$vv){
    $d_arr[$d] = 0;
    foreach($vv as $uid=>$uv){
    if(count($uv) == 1){
    $d_arr[$d]++; 
    }
    }
    }foreach($d_arr as $d=>$v){
    $res[] = array('num'=>$v,'dateTime'=>$d);
    }print_r($res);
    exit;
      

  5.   

    结果为:
    Array
    (
        [0] => Array
            (
                [num] => 2
                [dateTime] => 2010-11-17
            )    [1] => Array
            (
                [num] => 1
                [dateTime] => 2010-11-18
            ))
      

  6.   


    <?php
    $source = array( 
        0   =>  array('userId' => 1, 'thisUrl' => 'a.php', 'dataTime' => '2010-11-17 14:48:30'),
        1   =>  array('userId' => 1, 'thisUrl' => 'b.php', 'dataTime' => '2010-11-17 14:42:57'),
        2   =>  array('userId' => 2, 'thisUrl' => 'c.php', 'dataTime' => '2010-11-17 15:02:25'),
        3   =>  array('userId' => 2, 'thisUrl' => 'c.php', 'dataTime' => '2010-11-17 18:37:05'),
        4   =>  array('userId' => 5, 'thisUrl' => 'f.php', 'dataTime' => '2010-11-17 14:37:05'),
        5   =>  array('userId' => 4, 'thisUrl' => 'e.php', 'dataTime' => '2010-11-18 15:02:25'),
    );$result = array();
    foreach ($source as $k => $v)
    {
        $userId = $v['userId'];
        $dataTime = substr($v['dataTime'], 0, 10);
        $thisUrl = $v['thisUrl'];    if (isset($result[$userId]))
        {
            if (isset($result[$userId][$dataTime]))
            {
                if (!in_array($thisUrl, $result[$userId][$dataTime]['url']))
                {
                    $result[$userId][$dataTime]['count'] ++;
                    $result[$userId][$dataTime]['url'][] = $thisUrl;
                }
            }
            else
            {
                $result[$userId][$dataTime]['count'] = 1;
                $result[$userId][$dataTime]['url'][] = $thisUrl;
            }
        }
        else
        {
            $result[$userId] = array();
            if (isset($result[$userId][$dataTime]))
            {
                if (!in_array($thisUrl, $result[$userId][$dataTime]['url']))
                {
                    $result[$userId][$dataTime]['count'] ++;
                    $result[$userId][$dataTime]['url'][] = $thisUrl;
                }
            }
            else
            {
                $result[$userId][$dataTime]['count'] = 1;
                $result[$userId][$dataTime]['url'][] = $thisUrl;
            }
        }
    }
    $output = array();foreach ($result as $log)
    {
        foreach ($log as $k => $v)
        {
            if ($v['count'] == 1)
            {
                $output[$k] = isset($output[$k]) ? $output[$k] + 1 : 1;
            }
        }  
    }
    var_dump($output);
    ?>
      

  7.   

    哈哈~5楼的方法还是可以的!不过我这种懒人倒是想了个其他的办法!就是将原始的$arr_c数组单取userId一次,再单取dateTime一次~~求合集~~
    然后再用循环到数组里去逐个比对!代码就不写了~~手懒!
      

  8.   

    单取userId和dateTime,怎么关联它们两的关系,用数组下标么