All code: https://pastebin.com/5c317zU8

There is a php code. Which already works, you only need to organize the substitution in order from the beginning to the end of the array indices.

list($a1, $b1, $c1, $d1, $e1, $f1, $g1, $h1, $i1, $j1, $k1, $l1, $m1, $n1, $o1, $p1, $q1, $r1, $s1, $t1, $u1, $v1, $w1, $x1, $y1, $z1) = explode(', ', trim($file1[0], ', ')); 

Tried to do this using the key () function. Specifying the same trim ($ file1 [$ key], ',')

Which results in the error / Warning: trim () expects parameter 1 to be string, array given in

I understand that this means that I am trying to pass an array instead of a string. But how to do so, that would substitute the array index as a string from the beginning to the end of the array indices in order. Most likely this is done through the foreach function but I do not know how.

The result of the php script itself: https://pastebin.com/yPBXzgCd

It should be like this: https://pastebin.com/htGNCrFq

Updated:

Corrected code: https://pastebin.com/yJhMckLr

What happens: https://pastebin.com/KJ73WNSM

It should be like this: https://pastebin.com/htGNCrFq

So far, so I can not understand how to organize the processing of indices from beginning to end. By type from 0 to the n-th number of the index. That is, from the beginning to the end of the array indices.

  • The trim () function works with strings. The error says that $ file1 [0] is not a string, but an array. Make var_dump ($ file1 [0]) to understand what you are transmitting - RaZik
  • the result of running the var_dump ($ file [0]) / string (138) "4high_bkn, bkn_wi3,100, 12582912, 0, -41.469, -40.7371, -11.0741, 5.86037, 0.659833, 13.514, -17.8043, -20.0386, 1.21992, 33.7578 , null "Above, I already wrote that it gives an error when I try to put key as a variable for the place of the index. - Wilps Scrag
  • And in the code you have written $ file1 [0], $ file2 [0] - RaZik
  • Just left a working version. Not a working variant that leads to the error list ($ a1, $ b1, $ c1, $ d1, $ e1, $ f1, $ g1, $ h1, $ i1, $ j1, $ k1, $ l1, $ m1, $ n1 , $ o1, $ p1, $ q1, $ r1, $ s1, $ t1, $ u1, $ v1, $ w1, $ x1, $ y1, $ z1) = explode (',', trim ($ file1 [$ key], ',')); Attempt to substitute array indices. - Wilps Scrag
  • Fixed the code now with the key also works, but not as I need: pastebin.com/yJhMckLr It turns out: pastebin.com/KJ73WNSM And this is not what I need. The file itself which I am trying to convert to json: pastebin.com/8vGSSxa6 - Wilps Scrag

2 answers 2

perseverance and desire to achieve the goal is a good quality, but it is not necessary for this to write 26 variables (:

I understand that your task is to split the input string by separator. You are interested in, let's say the first 26 values, since so many variables you take. Although at the same time use only up to o (15). I hope you knew that you could not list all the variables that come from explode , but limit yourself to the 15th ones.

But that's not the point. You broke a string into 26 variables, and now you want to write them in the right places in a certain JSON, inserting them there manually.

I suggest you go the other way. Here, for example, a test string and numbering of elements in it (to check the result).

 // 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 $src = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; $input = explode(',', $src); 

Set the scheme, i.e. the structure of the generated json, where instead of the values ​​will be the indices of the resulting elements:

 $schema = [ "aabbMin" => [6,7,8], "aabbMax" => [9,10,11], "centroid" => [12,13,14], "radius" => 15, "archetypeName" => 1, "txdName" => 2, ]; 

And now recursively bypassing all the elements, replace the indices with the values ​​obtained:

 $data = $schema; array_walk_recursive($data, function(&$v) use ($input){ $v = $input[$v-1]; }); 

execution result

 Array ( [aabbMin] => Array ( [0] => F [1] => G [2] => H ) [aabbMax] => Array ( [0] => I [1] => J [2] => K ) [centroid] => Array ( [0] => L [1] => M [2] => N ) [radius] => O [archetypeName] => A [txdName] => B ) 

If such an operation should be carried out for all lines of the input file, then the whole thing successfully turns into a cycle:

  $lines = file("input.txt"); $schema = ...; $result = []; foreach($lines as $l){ $input = explode(...); $data = $schema; array_walk_recursive(....); $result[] = $data; } echo json_encode($result); 

    If I understood you correctly, then you read the first line of the file, which is some values ​​separated by a comma, dropped commas and spaces along the edges of this line and separated it, getting those same values.

    And now you want to do the same with each line of the file? PHP has foreach for this:

     foreach ($file as $line) { $vals = explode(', ', trim($line, ', ')); // ... } 

    As you can see, the $key variable is not required.

    I read your supplement and I want to draw attention to the fact that in the cycle you need to form your structure. Otherwise, the variables are simply overwritten and you get the result of only the last line.

    Note that JSON can be generated using the json_encode function:

     $t = []; foreach ($file1 as $line) { $vals = explode(', ', trim($line, ', ')); $t[] = [ 'aabbMin' => array_map('floatval', [$vals[5], $vals[6], $vals[7]]), 'aabbMax' => array_map('floatval', [$vals[8], $vals[9], $vals[10]]), 'centroid' => array_map('floatval', [$vals[11], $vals[12], $vals[13]]), 'radius' => floatval($vals[14]), 'archetypeName' => $vals[0], 'txdName' => $vals[1], ]; } $json = json_encode($t); echo $t; 
    • And how to form a similar structure? pp.userapi.com/c845420/v845420466/1e24f/TXrjQ0TsxEM.jpg At the moment, the structure is in the form of a solid line. And I'm afraid that there may be problems for the fact that everything is a solid line. When this data will be processed. That is, to organize the structure for each row. And do not write a solid line. Just wondering how to solve it. - Wilps Scrag
    • @WilpsScrag json_encode($t, JSON_PRETTY_PRINT) . But it only makes sense if JSON is meant to be read by humans. For the program, one line is not a problem. - Yegor Banin