如何在多维数组中计算数组中的总数

时间:2013-02-21 15:51:54

标签: php arrays multidimensional-array

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

此报告的信息类似于:

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'和位置:'方尖碑'。我尝试过多种方式,但我能想到的最好的是多维数组:

$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- (指数)id_errors id_event id_access_scanner id_errors_type total_errors

-errors_type- (指数)id_errors_type name_errors_type

-access_scanner- (指数)id_access_scanner id_portal name_access_scanner

请帮助!

谢谢!

4 个答案:

答案 0 :(得分: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";
}

答案 1 :(得分: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;
  }
}

答案 2 :(得分: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]++;
    }
}

答案 3 :(得分:0)

要完成每个结果,我只是做了以下事情:

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

现在将它连接到sql