-1

I have a text file with this format and I want to convert this into an array following the format.

Text File Content:

Header = "This is header"
Object class = "First"

min_size = 0 
max_size = 727.555 
content= true 
size = 1 
item []: 
    item [1]:
        class = "Convert" 
        name = "CONTENT" 
        min_size = 0 
        max_size = 727.555 
        intervals: size = 474 
        intervals [1]:
            min_size = 0 
            max_size = 13.139997023062838 
            type = "" 
        intervals [2]:
            min_size = 13.139997023062838 
            max_size = 14.763036269953904 
            type = "this is a type" 
        intervals [3]:
            min_size = 14.763036269953904 
            max_size = 17.01 
            type = "" 
        intervals [4]:
            min_size = 17.01 
            max_size = 18.193 
            type = "" 

I want to convert this to an array something like this if possible:

Array
(
    [1] => 
        Array(
                ['min_size'] = 0,
                ['max_size'] = 13.139997023062838,
                ['type'] = ""
            )
    [2] => 
        Array(
                ['min_size'] = 13.139997023062838,
                ['max_size'] = 14.763036269953904,
                ['type'] = "this is a type"
            )

I already tried something like this:

$file = "array.txt";// Your Temp Uploaded file
$cols = array();
ini_set('auto_detect_line_endings', true);

$fh = fopen($file, 'r');
$i = 0;

while (($line = fgetcsv($fh, 1000, "\t")) !== false) {
  $cols[] = $line;

  }

echo "<pre>";
print_r($cols); 
echo "</pre>";

But this only turned everything into a single array.

Is this possible?

I already looked into this: one two three

hungrykoala
  • 1,083
  • 1
  • 13
  • 28
  • Care to explain downvoter? – hungrykoala Dec 04 '17 at 12:20
  • Another downvote with no explanation whatsoever. Thank you – hungrykoala Dec 04 '17 at 12:25
  • _“Is this possible?”_ is not a proper kind of question to ask here. _“But this only turned everything into a single array”_ - well yes, because that’s all you did in that code. Of course you’re gonna need to do a bit more - like first of all find a way to identify which lines are the ones of interest. – CBroe Dec 04 '17 at 12:25
  • At least you commented. Thank you for your feedback. I was hoping there was a more straightforward function for this. I'm trying to check tabs now and convert it to array from there. – hungrykoala Dec 04 '17 at 12:26
  • `fgetcsv` is for reading data in CSV “format”. You don’t have that here, not even close. – CBroe Dec 04 '17 at 12:26
  • @CBroe Sorry, just saw that in an example and it was using a text file. – hungrykoala Dec 04 '17 at 12:27
  • I think those downvotes are groudless. This question deserves to be answered – RomanPerekhrest Dec 04 '17 at 12:27
  • @hungrykoala, while the solution is ready, the only nuance is keys in the result array: should they reflect the `intervals [1]` number or could go in natural order? I.e. should `intervals [2] ` be transformed into `[2] => Array()` ? – RomanPerekhrest Dec 04 '17 at 12:45
  • @RomanPerekhrest natural order would be fine. As long as they are presented in an array. Right now I'm trying to comb through the data with foreach and am having some trouble removing the whitespaces and tabs from the string so that `stristr` would be applicable for comparison. – hungrykoala Dec 04 '17 at 12:49

1 Answers1

1

Complex solution with preg_match_all, array_map and array_combine functions:

$data = file_get_contents('array.txt');
$pat = '/intervals \[\d+\]:\s+\Kmin_size = (?P<min_size>\d+(\.\d+)?) \
        \s+max_size = (?P<max_size>\d+(\.\d+)?)\s+type = "(?P<type>[^"]*)"/m';

preg_match_all($pat, $data, $m);
$result = array_map(function($a){
    return array_combine(['min_size', 'max_size', 'type'], $a);
}, array_map(null, $m['min_size'], $m['max_size'], $m['type']));

print_r($result);

The output:

Array
(
    [0] => Array
        (
            [min_size] => 0
            [max_size] => 13.139997023062838
            [type] => 
        )

    [1] => Array
        (
            [min_size] => 13.139997023062838
            [max_size] => 14.763036269953904
            [type] => this is a type
        )

    [2] => Array
        (
            [min_size] => 14.763036269953904
            [max_size] => 17.01
            [type] => 
        )

    [3] => Array
        (
            [min_size] => 17.01
            [max_size] => 18.193
            [type] => 
        )
)
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • Thank you for this. It works but I had a problem with my file I had to change the encoding to UTF-8 for this to work. – hungrykoala Dec 04 '17 at 13:00