我正在尝试从我的日志中删除重复的时间戳,从而必须将重复行中的数组值连接到前一个数组行的值中。.
到目前为止,我已经设法删除了重复的时间戳。但是,如果在该特定数组中不存在datestamp,我很难将数组值连接到先前的值中。
请帮助我将值压缩到以前的数组中?
日志文件遵循以下格式:
20160420084726:-
20160420085418:[111783178, 111557953, 111646835, 111413356, 111412662, 105618372, 111413557]
20160420085418:[111413432, 111633904, 111783198, 111792767, 111557948, 111413225, 111413281]
20160420085418:[111413432, 111633904, 111783198, 111792767, 111557948, 111413225, 111413281]
20160420085522:[111344871, 111394583, 111295547, 111379566, 111352520]
20160420090022:[111344871, 111394583, 111295547, 111379566, 111352520]
如何删除重复的时间戳,同时将值保留在前一行中,同时保留最后的压缩值/ n换行符。
<?php
$filename = "datesandids.log";
$lines = explode("\n", file_get_contents($filename));
print_r($lines);
$count = count($lines);
for ($i = 0;$i < $count;$i++) {
$lineattr = explode(":", $lines[$i]);
echo $lines[$i];
}
foreach ($lines as $linevar => $row) {
$line = explode(":", $linevar);
print_r($lines);
}
$handle = fopen("$filename", "r");
$datevar = array();
while ($line = fgets($handle)) {
$line = trim($line);
$data = explode(":", $line);
$datevar[] = $data;
}
var_dump($datevar);
$found = array();
function concatArray($array, $keys, $glue = " ") {
$last = null;
foreach ($array as $key => & $value) {
if (in_array($key, $keys)) {
if ($last === null) {
$last = $key;
continue;
}
$array[$last] .= $glue . $value;
unset($array[$key]);
}
}
return $array;
}
foreach ($datevar as $i => $x) {
if (in_array($x[0], $found)) {
$datevar[$i][0] = '';
$date = array(
$datevar
);
print_r(concatArray($date, array(
0,
1
)));
}
else {
$found[] = $x[0];
}
}
print_r($datevar);
foreach ($datevar as $key => $value) {
$datevar[$key] = preg_replace("/\[/", "", $value);
}
foreach ($datevar as $key => $value) {
$datevar[$key] = preg_replace("/\]/", "", $value);
}
print_r($datevar);
?>
我正在尝试生成以下数组信息:
Array(
[0] => Array(
[0] => 20160420084726
[1] => -
)
[1] => Array(
[0] => 20160420085418
[1] => 111783178, 111557953, 111646835, 111413356, 111412662, 105618372, 111413557, 111413432, 111633904, 111783198, 111792767, 111557948, 111413225, 111413281, 111413432, 111633904, 111783198, 111792767, 111557948, 111413225, 111413281
)
[2] => Array(
[0] => 20160420085522
[1] => 111344871, 111394583, 111295547, 111379566, 111352520
)
[3] => Array(
[0] => 20160420090022
[1] => 111344871, 111394583, 111295547, 111379566, 111352520
)
)
我实际上正在得到:
Array(
[0] => Array(
[0] => 20160420084726
[1] => -
)
[1] => Array(
[0] => 20160420085418
[1] => 111783178, 111557953, 111646835, 111413356, 111412662, 105618372, 111413557
)
[2] => Array(
[0] =>
[1] => 111413432, 111633904, 111783198, 111792767, 111557948, 111413225, 111413281
)
[3] => Array(
[0] =>
[1] => 111413432, 111633904, 111783198, 111792767, 111557948, 111413225, 111413281
)
[4] => Array(
[0] => 20160420085522
[1] => 111344871, 111394583, 111295547, 111379566, 111352520
)
[5] => Array(
[0] => 20160420090022
[1] => 111344871, 111394583, 111295547, 111379566, 111352520
)
)
答案 0 :(得分:0)
您可以尝试这样的方法:
<?php
$filename = "datesandids.log";
$rows = explode(PHP_EOL, file_get_contents($filename));
$result = Array();
foreach($rows as $key => $row){
$keyval = explode(':', $row);
$keyval[1] = str_replace('[', '', $keyval[1]);
$keyval[1] = str_replace(']', '', $keyval[1]);
$keyval[1] = explode(', ', trim($keyval[1]));
if(!isset($result[$keyval[0]])){
$result[$keyval[0]] = $keyval[1];
}else{
$result[$keyval[0]] = array_merge($result[$keyval[0]], $keyval[1]);
}
}
var_dump($result);
PHP中的数组实际上是有序映射。地图是一种 将值与键关联。此类型针对以下几种进行了优化 不同用途;可以将其视为数组,列表(向量),哈希 表格(地图的实现),字典,集合,堆栈, 排队,甚至更多。由于数组值可以是其他数组,所以树 多维数组也是可能的。
通过这种方式,您不必担心重复太多
此外,我建议您使用PHP documentation爆炸文件,这样您就不会在其他系统上遇到麻烦了
输出将是这样的:
array(4){
[20160420084726]=> array(1) {
[0]=> string(1) "-"
}
[20160420085418]=> array(21) {
[0]=> string(9) "111783178"
.
.
[20]=> string(9) "111413281"
}
[20160420085522]=> array(5) {
[0]=> string(9) "111344871"
.
.
[4]=> string(9) "111352520"
}
[20160420090022]=> array(5) {
[0]=> string(9) "111344871"
.
.
[4]=> string(9) "111352520"
}
}