TheEmperor TheEmperor - 6 months ago 15
PHP Question

Modify lines in a text file and save into a new array PHP

I have the following text file (Query1.cif):

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


What I would like to do is to is:

1) Read each line in the text file

2) Check the last column. If the value is not 1, change it to 1

3) Save all the lines into a new array.

I'm having problem with the third step. I'm not sure of the proper way of saving each line into a new array. I also need to access the array values outside the foreach loop.

Here is the link of the code 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

Answer

I got this working well enough. Please note that I use the string format because I didn't want to create a file to do this with. You should still use file_get_contents.

Code

$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 = [];
foreach ($Query1 as $line) {

    $line = trim($line);
        if(strlen($line)==0) continue;
    $last_char = substr($line,-1);
        if($last_char !== '1'){
            $line = substr_replace($line,'1',-1);
        }

    $new_arr[] = explode(' ',$line);


// 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

Output

This is the output of the print_r call in the 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
        )

)