我有一个数组$session
,我从awstats文件中提取:
# Session range - Number of visits
BEGIN_SESSION 7
1h+ 10
5mn-15mn 9
0s-30s 107
2mn-5mn 7
30s-2mn 21
15mn-30mn 4
30mn-1h 11
END_SESSION
首先,我想通过添加0s-30s
&的两个值来重新安排此操作。 30s-2mn
并创建另一个,这是我尝试的方式:
$newline="\n";
$lines = explode($newline,$session);
$results = array();
foreach($lines as $line) {
$parts = explode(" ",trim($line),2);
if( count($parts) < 2) continue;
else {
$results[$parts[0]] = intval($parts[1]);
}
}
$temp['0s-30s'] = (isset($results['0s-30s'])?$results['0s-30s']:NULL);
$temp['30s-2mn'] = (isset($results['30s-2mn'])?$results['30s-2mn']:NULL);
$results['0s-2mn'] = $temp['0s-30s'] + $temp['30s-2mn'];
unset($results['0s-30s'],$results['30s-2mn']);
$session = $results['BEGIN_SESSION'].$newline;
foreach($results as $k=>$v) $session .= $k." ".$v.$newline;
$session .= "END_SESSION";
$session = explode("\n", $session) ;
unset($session[(count($session)-1)]) ;
unset($session[0]) ;
unset($session[1]) ;
$sessions = array();
foreach ($session as $key => $value) {
$session[$key] = explode(" ", $value) ;
$sessions[] = array($session[$key][0],trim($session[$key][1])) ;
}
它显示了这个数组:
Array
(
[0] => Array
(
[0] => 1h+
[1] => 10
)
[1] => Array
(
[0] => 5mn-15mn
[1] => 9
)
[2] => Array
(
[0] => 2mn-5mn
[1] => 7
)
[3] => Array
(
[0] => 15mn-30mn
[1] => 4
)
[4] => Array
(
[0] => 30mn-1h
[1] => 11
)
[5] => Array
(
[0] => 0s-2mn
[1] => 128
)
)
有没有办法重新排列我的数组:
Array
(
[0] => Array
(
[0] => 1h+
[1] => 10
)
[1] => Array
(
[0] => 30mn-1h
[1] => 11
)
[2] => Array
(
[0] => 15mn-30mn
[1] => 4
)
[3] => Array
(
[0] => 5mn-15mn
[1] => 9
)
[4] => Array
(
[0] => 2mn-5mn
[1] => 7
)
[5] => Array
(
[0] => 0s-2mn
[1] => 128
)
)
知道$session
有时会缺少会话。对此有何帮助?感谢。
答案 0 :(得分:1)
<?php
$session = 'BEGIN_SESSION 7
1h+ 10
5mn-15mn 9
0s-30s 107
2mn-5mn 7
30s-2mn 21
15mn-30mn 4
30mn-1h 11
END_SESSION';
$newline="\n";
$lines = explode($newline,$session);
$results = array();
foreach($lines as $line) {
$parts = explode(" ", trim($line), 2);
if (in_array($parts[0], array('BEGIN_SESSION', 'END_SESSION'))) continue;
else $results[$parts[0]] = intval($parts[1]);
}
$temp['0s-30s'] = isset($results['0s-30s']) ? $results['0s-30s'] : 0;
$temp['30s-2mn'] = isset($results['30s-2mn']) ? $results['30s-2mn'] : 0;
$results['0s-2mn'] = $temp['0s-30s'] + $temp['30s-2mn'];
unset($results['0s-30s'], $results['30s-2mn']);
$order = array('0s-2mn', '2mn-5mn', '5mn-15mn', '15mn-30mn', '30mn-1h', '1h+');
uksort($results, function($a, $b) use ($order) {
return array_search($a, $order) < array_search($b, $order);
});
var_dump($results);