2017-04-06 1 views
0

ersten Mal Rückgriff auf tatsächlich Buchung auf SO. Es tut mir auch leid, wenn das oft gefragt wurde, ich denke ich lese über die meisten von ihnen hier, aber immer noch keine Würfel.Problem mit doppelten Array-Werten von preg_match_all

Ich habe eine generierte Protokolldatei continating Text Ich möchte die Zeile in der Protokolldatei ist dies: {22:30:47} System: "Obambivas" StarPos: (- 59.938,7.375,56.813) LY Körper : 13 RelPos: (- 0.529636, -0.130899, 0.838064) km NormalFlight

Bis jetzt habe ich versucht, die Spiele über preg_match_all zu bekommen, und funktioniert gut. Allerdings brauche ich wirklich jedes System: "" Nur einmal wie das Protokoll mehrere genau das gleiche haben kann.

Ive versuchte array_unique zu verwenden, aber ich bin ziemlich sicher, dass im es falsch verwendet, da sie entweder nichts oder die gleichen Ergebnisse Retruns, dh 10+ für jedes Spiel Spiele gefunden

Also muss ich nur jede einzelne Spiel von den Spielen in der Protokolldatei gefunden.

Mein Code so weit (sorry, wenn seine chaotisch) Und Dank im Voraus

if (is_dir($log) && is_readable($log)) { 


if (!$files = scandir($log, SCANDIR_SORT_DESCENDING)) { 

} 
$newest_file = $files[0]; 

if (!$line = file($log . "/" . $newest_file)) { 

} else { 

    foreach ($line as $line_num => $line) { 


     $pos = strpos($line, 'System:"'); 

     $pos2 = strrpos($line, "ProvingGround"); 

     if ($pos !== false && $pos2 === false) { 

      preg_match_all("/\System:\"(.*?)\"/", $line, $matches); 
      $cssystemname = $matches[1][0]; 
      $curSys["name"] = $cssystemname; 


      preg_match_all("/\StarPos:\((.*?)\)/", $line, $matches2); 
      $curSys["coordinates"] = $matches2[1][0]; 
      $coord_parts = explode(",", $curSys["coordinates"]); 

      $curSys["x"] = $coord_parts[0]; 
      $curSys["y"] = $coord_parts[1]; 
      $curSys["z"] = $coord_parts[2]; 

      echo $curSys["name"].' | Coords: '.$curSys["x"].','.$curSys["y"].','.$curSys["z"].'<br />'; 

     } 

    } 
} 

}

+0

Statt vollständigen Code, Bitte geben Sie Ihre Eingabe und die erwartete Ausgabe an. –

+0

Danke und ich werde in Zukunft, sorry. –

Antwort

0

Ich habe $ hash Array Duplikate

zu vermeiden
if (is_dir($log) && is_readable($log)) { 


if (!$files = scandir($log, SCANDIR_SORT_DESCENDING)) { 

} 
$newest_file = $files[0]; 

if (!$line = file($log . "/" . $newest_file)) { 

} else { 

    $hash = array(); 

    foreach ($line as $line_num => $line) { 


    $pos = strpos($line, 'System:"'); 

    $pos2 = strrpos($line, "ProvingGround"); 

    if ($pos !== false && $pos2 === false) { 

     preg_match_all("/\System:\"(.*?)\"/", $line, $matches); 
     $cssystemname = $matches[1][0]; 
     if ($hash[$cssystemname] == "") 
     { 
      $curSys["name"] = $cssystemname; 


      preg_match_all("/\StarPos:\((.*?)\)/", $line, $matches2); 
      $curSys["coordinates"] = $matches2[1][0]; 
      $coord_parts = explode(",", $curSys["coordinates"]); 

      $curSys["x"] = $coord_parts[0]; 
      $curSys["y"] = $coord_parts[1]; 
      $curSys["z"] = $coord_parts[2]; 

      echo $curSys["name"].' | Coords: '.$curSys["x"].','.$curSys["y"].','.$curSys["z"].'<br />'; 

    } 
    } else $hash[$cssystemname] = "inhash"; 
} 
} 
+0

Danke für die schnelle Antwort, das funktioniert gut, ich sehe nur jede eindeutige Übereinstimmung jetzt, aber ich bekomme Php Hinweise, die erste ist: Hinweis: Undefinierte Variable: csystemname in C: \ wamp64 \ www \ elite \ update_logs.php on line 71 und zweite: Hinweis: Undefinierter Index: Obambivas in C: \ wamp64 \ www \ elite \ update_logs.php on line 55 Abgesehen von diesen funktioniert es gut, danke. –

+0

ersetzen if ($ hash [$ csystemname] == "") zu if (! Isset ($ hash [$ csystemname])) – diavolic

+0

Danke, das ist das Hash-Array-Index-Problem sortiert. –