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
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
解决方案 »
- php5调用外部C库,急!
- 关于php中把html代码保存到oracle中的问题
- PHP文件中的<script src='..'> </scirpt>中内容不能显示
- shtml文件中包含2个php文件,apache就报错
- 彭和平垃圾回收机制
- 这个例子倒底错在哪?
- 急急急,在线等。php怎么就是从access中查询不出来呢?
- 如何根据变量内容自动提交对应的表单?
- CGI的TimeOut问题!
- 帮帮忙各位大佬.获取POST数据写入图像问题
- flex 通过amfphp连接mqsql数据库 汉字编码问题
- preg_match_all("/ab(.*?)d/","abcabcbcd",$out); 结果是?
foreach($arr as $k=>$v){
//将$v截取出来日期
将日期作为key重组一下数组 如果存在 num+1
}
数组应该为
Array (
[0] => Array ( [num] => 2 [dateTime] => 2010-11-17 )
[1] => Array ( [num] => 1 [dateTime] => 2010-11-18 )
)
11-17 应该是二个,
这如何实现 呀thanks
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;
Array
(
[0] => Array
(
[num] => 2
[dateTime] => 2010-11-17
) [1] => Array
(
[num] => 1
[dateTime] => 2010-11-18
))
<?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);
?>
然后再用循环到数组里去逐个比对!代码就不写了~~手懒!