Hello! I break my head for 3 days, the question is as follows: there is a data file in the form:

 G1 = 16470996
 S1020 G1 = 12298521 G2 = 4773652 G3 = 961877 G4 = 90638 G5 = 3155073 G6 = 3153681 G7 = 1908 G8 = 1027 G9 = 654794 G10 = 1321424 G11 = 391534 G12 = 441279 G13 = 115151616 G12 = 441279 G13 = 115711616 G12 = 441279 G13 = 1151516161616
 S1030 G1 = 2172607 G2 = 733154 G3 = 130659 G4 = 12325 G5 = 511283 G6 = 511280 G7 = 40 G8 = 14 G9 = 91172 G10 = 9537 G11 = 6538 G12 = 98899 G13 = 5415 G14 = 4536 G15 = 494 G16 = 1318570

I convert it to the array so $array = explode("/n", file_get_contents('file.ini'));

But the bottom line is that you need to further add variables - they are numbers

For example: you need to take the line S1020 and the number that is after G3, that is, 961877, then add this number with the variable from the line S1030 G12, that is, 98899 and so on ...

In general, I do not understand how to create these variables and how to access them. Ideally, you should get something in the form

$ p1 = ($ s1010G1 + $ s2010G2 + $ s4010G4 + $ s4210G16) - ($ s5010G4 + $ s5200G3);

where variables are formed from different lines $ s1010G1 = s1010 line; G1 variable.

As a result, the whole stalled on the array of the form:

     S1010
         [10] => G1 = 16470996
         [11] => G2 = 6410840
         [12] => G3 = 1256281
         [13] => G4 = 116381
         [14] => G5 = 4186435
         [15] => G6 = 4184187
         [16] => G7 = 2219
         [17] => G8 = 1069
         [18] => G9 = 965905
         [19] => G10 = 1835111
         [20] => G11 = 552534
         [21] => G12 = 649459
         [22] => G13 = 32415
         [23] => G14 = 25894
         [24] => G15 = 7314
         [25] => G16 = 6983323
         [26] => 
     S1020
         [27] => G1 = 12298521
         [28] => G2 = 4773652
         [29] => G3 = 961877
         [30] => G4 = 90638
         [31] => G5 = 3155073
         [32] => G6 = 3153681
         [33] => G7 = 1908
         [34] => G8 = 1027
         [35] => G9 = 654794
         [36] => G10 = 1321424
         [37] => G11 = 391534
         [38] => G12 = 441279
         [39] => G13 = 11571
         [40] => G14 = 9431
         [41] => G15 = 2461
         [42] => G16 = 5356600
         [43] => 

  • Why so confusing .... - nick_n_a
  • And what exactly is not clear? - Dmitry

1 answer 1

Once you have already converted a set of strings into an array, then convert the strings themselves? and perform calculations on the elements of the array.

 //$lines = file("input.txt"); $lines = explode("\n", $txt); $data = []; foreach($lines as $l){ $tmp = explode(' ', $l); $key = array_shift($tmp); $data[$key] = array_map(function($e){ list($k,$v) = explode("=",$e); return $v; }, $tmp); } print_r($data); 

at the output, get an array of the form

 Array( [S1010] => Array ( [0] => 16470996 [1] => 6410840 [2] => 1256281 [3] => 116381 [4] => 4186435 [5] => 4184187 [6] => 2219 [7] => 1069 [8] => 965905 [9] => 1835111 [10] => 552534 [11] => 649459 [12] => 32415 [13] => 25894 [14] => 7314 [15] => 6983323 ) .... 

and your math will obviously be $data['s1010'][0] + ...


Of course, you can bring this whole thing to variables, if you like, it can hardly be considered a practical approach. For these purposes, it is necessary to prepare an array of variables, and use the exctract function:

 foreach($lines as $l){ $tmp = explode(' ', $l); $key = array_shift($tmp); foreach($tmp as $x){ list($g, $v) = explode("=", $x); $data[$key.$g] = $v; } } extract($data); print_r($S1010G1 + $S1010G2); 
  • Here is thanks, kind man! I knew that everything was so simple, but I went the other way, started working with strings ... Thank you! - Dmitry
  • one
    @Dmitry well then you can tick the answer - teran