2016-06-01 11 views
0

Ich habe die folgende Textdatei (Query1.cif):Ändern Zeilen in einer Textdatei und speichern in ein neues Array PHP

ATOM 1 P P  . A A 1 1 1 ? 25.393 -14.093 8.181 1.00 55.25 ? ? ? ? ? ? ? 23 A X P  23 A X P  2 

ATOM 2 O OP1 . A A 1 1 1 ? 25.462 -13.992 9.643 1.00 56.56 ? ? ? ? ? ? ? 23 A X OP1 23 A X OP1 2 

ATOM 3 O OP2 . A A 1 1 1 ? 25.063 -12.918 7.334 1.00 53.05 ? ? ? ? ? ? ? 23 A X OP2 23 A X OP2 1 

Was würde Ich mag es zu tun ist:

1) Lesen Sie jede Zeile in der Textdatei

2) Überprüfen Sie die letzte Spalte. Wenn der Wert nicht 1 ist, ändern Sie ihn in 1

3) Speichern Sie alle Zeilen in einem neuen Array.

Ich habe Probleme mit dem dritten Schritt. Ich bin mir nicht sicher, wie man jede Zeile in einem neuen Array speichern kann. Ich muss auch auf die Array-Werte außerhalb der foreach-Schleife zugreifen. Hier

ist die Verbindung des Codes in Pastebin - http://pastebin.com/PLY9FaLN

//Query 1 would be an array containing the atomic coordinates of motifs/nucleotides of interest 

$Query1 = explode("\n", file_get_contents('Query1.cif')); 

foreach ($Query1 as $line) { 

    $line = trim($line); 

    $line = (explode(" ",$line)); 

    $last_index = count($line) - 1; 

    echo "</br>"; echo "</br>"; 

     if (strcmp($line[$last_index], '1') !== 0) { 
      //echo '$var1 is not equal to $var2 in a case sensitive string comparison'."</br>"; 
      $line = str_replace ($line[$last_index], '1', $line); 

     } 

// I need to save each line into an array and access them outside the loop 

} 

// I want to access the array values here 
+0

an ein PHP-Array hinzuzufügen, $ array [] = $ neues Mitglied; – Ken

+0

Strlen() gibt die Länge einer Zeichenfolge an. Also sollte substr ($ line, strlen ($ line) -1, strlen ($ line)) das letzte Zeichen der Zeile sein. – Andreas

Antwort

0

Ich habe dies gut genug arbeiten. Bitte beachten Sie, dass ich das String-Format verwende, da ich dafür keine Datei erstellen wollte. Sie sollten immer noch file_get_contents verwenden.

-Code

<?php 
$query_string = <<<EOD 
ATOM 1 P P  . A A 1 1 1 ? 25.393 -14.093 8.181 1.00 55.25 ? ? ? ? ? ? ? 23 A X P  23 A X P  2 

ATOM 2 O OP1 . A A 1 1 1 ? 25.462 -13.992 9.643 1.00 56.56 ? ? ? ? ? ? ? 23 A X OP1 23 A X OP1 2 

ATOM 3 O OP2 . A A 1 1 1 ? 25.063 -12.918 7.334 1.00 53.05 ? ? ? ? ? ? ? 23 A X OP2 23 A X OP2 1 
EOD; 

$Query1 = explode("\n", $query_string); 
$new_arr = []; 
//Creates a new array; 
foreach ($Query1 as $line) { 

    $line = trim($line); 
     //trims the line of any extra spaces 
     if(strlen($line)==0) continue; 
     //If this is one of the lines with no characters on it (like the ones between ATOM 1 and ATOM 2, or ATOM 2 and ATOM 3), skip this particular loop 
     //aka "continue" the loop. 
    $last_char = substr($line,-1); 
     //Gets the last character with the substr() function. providing -1 starts at the first character at the end of the string. 
     if($last_char !== '1'){ 
      //If the last line is a 1, we go into this if 
      $line = substr_replace($line,'1',-1); 
      //Replace the substring content of $line, starting at the first character at the end, with '1'. 
      //This overwrites the last character. 
     } 
    $new_arr[] = explode(' ',$line); 
    //$new_arr[] adds whatever is on the other side of the equal sign to the next index iteration in the array. 
    //The first call adds the content on the other line to $new_arr[0], the second loop will add it to $new_arr[1], etc. 


// I need to save each line into an array and access them outside the loop 

} 
echo '<pre>'; 
    print_r($new_arr); 
echo '</pre>'; 
// I want to access the array values here 
?> 

Ausgabe

Dies ist der Ausgang des print_r Aufruf im Browser:

Array 
(
    [0] => Array 
     (
      [0] => ATOM 
      [1] => 1 
      [2] => 
      [3] => P 
      [4] => P 
      [5] => 
      [6] => 
      [7] => 
      [8] => 
      [9] => . 
      [10] => A 
      [11] => A 
      [12] => 1 
      [13] => 1 
      [14] => 1 
      [15] => ? 
      [16] => 25.393 
      [17] => -14.093 
      [18] => 8.181 
      [19] => 
      [20] => 1.00 
      [21] => 55.25 
      [22] => ? 
      [23] => ? 
      [24] => ? 
      [25] => ? 
      [26] => ? 
      [27] => ? 
      [28] => ? 
      [29] => 23 
      [30] => A 
      [31] => X 
      [32] => P 
      [33] => 
      [34] => 
      [35] => 
      [36] => 
      [37] => 23 
      [38] => A 
      [39] => X 
      [40] => P 
      [41] => 
      [42] => 
      [43] => 
      [44] => 
      [45] => 1 
     ) 

    [1] => Array 
     (
      [0] => ATOM 
      [1] => 2 
      [2] => 
      [3] => O 
      [4] => OP1 
      [5] => 
      [6] => 
      [7] => . 
      [8] => A 
      [9] => A 
      [10] => 1 
      [11] => 1 
      [12] => 1 
      [13] => ? 
      [14] => 25.462 
      [15] => -13.992 
      [16] => 9.643 
      [17] => 
      [18] => 1.00 
      [19] => 56.56 
      [20] => ? 
      [21] => ? 
      [22] => ? 
      [23] => ? 
      [24] => ? 
      [25] => ? 
      [26] => ? 
      [27] => 23 
      [28] => A 
      [29] => X 
      [30] => OP1 
      [31] => 
      [32] => 
      [33] => 23 
      [34] => A 
      [35] => X 
      [36] => OP1 
      [37] => 
      [38] => 
      [39] => 1 
     ) 

    [2] => Array 
     (
      [0] => ATOM 
      [1] => 3 
      [2] => 
      [3] => O 
      [4] => OP2 
      [5] => 
      [6] => 
      [7] => . 
      [8] => A 
      [9] => A 
      [10] => 1 
      [11] => 1 
      [12] => 1 
      [13] => ? 
      [14] => 25.063 
      [15] => -12.918 
      [16] => 7.334 
      [17] => 
      [18] => 1.00 
      [19] => 53.05 
      [20] => ? 
      [21] => ? 
      [22] => ? 
      [23] => ? 
      [24] => ? 
      [25] => ? 
      [26] => ? 
      [27] => 23 
      [28] => A 
      [29] => X 
      [30] => OP2 
      [31] => 
      [32] => 
      [33] => 23 
      [34] => A 
      [35] => X 
      [36] => OP2 
      [37] => 
      [38] => 
      [39] => 1 
     ) 

) 
+0

Danke für die Lösung, es hat wie ein Charme funktioniert! Ich würde es begrüßen, wenn Sie ein wenig über die if-Schleife erklären könnten. – TheEmperor

+0

@TheKaiser überprüfen Sie die Kommentare in der Bearbeitung. – Jhecht

+0

Danke! Das ist sehr hilfreich! – TheEmperor

0
$Query1 = explode("\n", file_get_contents('Query1.cif')); 
$newarray =""; //create new array 

foreach ($Query1 as $line) { 

    $line = trim($line); 

    If (substr($line, strlen($line)-1, strlen($line)) != 1) $line = substr($line, 0, strlen($line)-1)."1"; 

    $newarray[] = $line; 

} 

Var_dump($newarray); 
Verwandte Themen