0

我有一个来自报告的数组。

该报告的信息类似于:

157479877294,OBSOLETE_ORDER,obelisk,19/01/2013 01:42pm
191532426695,WRONG_PERFORMANCE,g3t1,19/01/2013 01:56pm
159523681637,WRONG_PERFORMANCE,g3t1,19/01/2013 01:57pm
176481653889,WRONG_PERFORMANCE,g4t1,19/01/2013 01:57pm
167479810401,WRONG_PERFORMANCE,g4t1,19/01/2013 02:00pm
172485359309,WRONG_PERFORMANCE,g4t2,19/01/2013 02:02pm
125485358802,WRONG_PERFORMANCE,g4t2,19/01/2013 02:02pm
172485359309,DAY_LIMIT_EXCEEDED,obelisk,19/01/2013 02:03pm
125485358802,DAY_LIMIT_EXCEEDED,obelisk,19/01/2013 02:03pm

我需要做的是获取每种错误类型的总数和位置,因此第一个错误是:'OBSOLETE_ORDER' 和位置:'obelisk'。我尝试了多种方法,但我能想到的最好方法是多维数组:

$error_handle = fopen("$reportUrl", "r");
while (!feof($error_handle) )
   {
      $line_of_text = fgetcsv($error_handle, 1024);
      $errorName = $line_of_text[1];
      $scannerName = $line_of_text[2];
      if($errorName != "SCAN_RESULT" && $errorName != "" && $scannerName != "SCAN_LOGIN" && $scannerName != "")
          {
              $errorsArray["$errorName"]["$scannerName"]++;
          }
   }
fclose($error_handle);
print_r($errorsArray);

给了我以下内容:

Array ( [OBSOLETE_ORDER] => Array ( [obelisk] => 1 ) [WRONG_PERFORMANCE] => Array ( [g3t1] => 2 [g4t1] => 2 [g4t2] => 2 ) [DAY_LIMIT_EXCEEDED] => Array ( [obelisk] => 2 ) )

这太棒了......除了我如何把它分开添加到我的 sql 数据库中?!(我有兴趣在数组所在的键下获取该键的键和总数)

然后将其添加到表中

-errors- (index)id_errors id_event id_access_scanner id_errors_type total_errors

-errors_type- (index)id_errors_type name_errors_type

-access_scanner- (index)id_access_scanner id_portal name_access_scanner

请帮忙!

谢谢!

4

4 回答 4

0

多维数组超出了您的需要。采取的方法是创建自己的字符串($arrayKey在我的示例中)以用作组合扫描仪名称和错误的数组键,以便您可以获得计数。

//this is the array containing all the report lines, each as an array
$lines_of_text;

//this is going to be our output array
$errorScannerArray = array();

//this variable holds the array key that we're going to generate from each line
$arrayKey = null;

foreach($lines_of_text as $line_of_text)
{
    //the array key is a combination of the scanner name and the error name
    //the tilde is included to attempt to prevent possible (rare) collisions
    $arrayKey = trim($line_of_text[1]).'~'.trim($line_of_text[2]);

    //if the array key exists, increase the count by 1
    //if it doesn't exist, set the count to 1
    if(array_key_exists($arrayKey, $errorScannerArray))
    {
        $errorScannerArray[$arrayKey]++;
    }
    else
    {
        $errorScannerArray[$arrayKey] = 1;
    }
}

//clean up
unset($line_of_text);
unset($arrayKey);
unset($lines_of_text);

//displaying the result
foreach($errorScannerArray as $errorScanner => $count)
{
    //we can explode the string hash to get the separate error and scanner names
    $names = explode('~', $errorScanner);
    $errorName = $names[0];
    $scannerName = $names[1];

    echo 'Scanner '.$scannerName.' experienced error '.$errorName.' '.$count.' times'."\n";
}
于 2013-02-21T16:48:33.717 回答
0

为了查看每个结果,我只做了以下操作:

foreach ($errorsArray as $errorName=>$info)
    {
        foreach ($info as $scannerName=>$total)
            {
                print "$errorName -> $scannerName = $total </br>";
            }
    }

现在只需将其连接到 sql

于 2013-02-22T00:18:33.343 回答
0

使用您编辑的问题,这个更简单的循环将为您工作,您只需将数据插入循环内的数据库中,而不是回显它:

$errorsArray = Array (
        [OBSOLETE_ORDER] => Array (
                                    [obelisk] => 1 
                                )
        [WRONG_PERFORMANCE] => Array (
                                    [g3t1] => 2 
                                    [g4t1] => 2 
                                    [g4t2] => 2 
                                ) 
        [DAY_LIMIT_EXCEEDED] => Array ( 
                                    [obelisk] => 2 
                                ) 
    )

foreach($errorsArray as $row => $errors) {
    foreach($errors as $error => $count) {
        echo $row;  // 'OBSOLETE_ORDER'
        echo $error;    // 'obelisk'
        echo $count;    // 1
        // insert into database here
    }
}

旧答案您只需要一个新数组来保存您需要的信息,理想情况下是一个计数。我假设正确的数据格式是:

$report = [
  ['157479877294','OBSOLETE_ORDER','obelisk','19/01/2013 01:42pm'],
  ['191532426695','WRONG_PERFORMANCE','g3t1','19/01/2013 01:56pm'],
  ['159523681637','WRONG_PERFORMANCE','g3t1','19/01/2013 01:57pm'],
  ['176481653889','WRONG_PERFORMANCE','g4t1','19/01/2013 01:57pm'],
  .....
];

foreach($report as $array) {
  $errorName = $array[1];
  $scannerName = $array[2];
  if(exists($errorsArray[$errorName][$scannerName])) {
    $errorsArray[$errorName][$scannerName] = $errorsArray[$errorName][$scannerName] + 1;
  }
  else {
    $errorsArray[$errorName][$scannerName] = 1;
  }
}
于 2013-02-21T17:03:34.800 回答
0
$list = array();
foreach ($lines as $line) {
    $values = explode(',' $line);
    $error = $values[1];
    $scanner = $values[2];
    if (!isset($list[$error])) {
         $list[$error] = array();
    }
    if (!isset($list[$error][$scanner])) {
         $list[$error][$scanner] = 1;
    } else {
         $list[$error][$scanner]++;
    }
}
于 2013-02-21T17:04:18.367 回答