0

I'm trying the following,

I have different files (8 files) that stores values like this:

1      2      3      4      5      6
7      8      9      10     11     12
....................

I would like to read 8 files at the same time and create an array that will store the first value of each file, another array with the second element, and so on.

For example, if first element of file1 is 1, in file2 8, ..., in file8 23; the resulting array in this iteration would be:

first_array = [1, 8, ....., 23]

I was making some tests on reading files in PHP like this:

$myfile = fopen("textoPrueba.txt", "r", 
"/home/berni/Documentos/Vesta1");
// Output one character until end-of-file
while(!feof($myfile))
{
   echo fgetc($myfile);
}
fclose($myfile);

This code just show me the elements of a file, but I would like to take specific elements in an iteration.

Someone can give me a hint? Thanks in advance

(NOTE: files have more than a million of elements)

5
  • 4
    My best hint is to use a database; that's what they're made for. Now if I understand correctly, each file has numbers separated by spaces? If so, read the first 5 or so characters, put it in a string and explode on space. then take the first array element. Commented Jun 9, 2019 at 21:04
  • Thank you for your answer, but first I'd like to learn reading from different files to create the array I need, because making an insert of 8 million of registers in a database I see so excesive.Thank you again! Commented Jun 9, 2019 at 21:08
  • 3
    The initial load of data might take a while, but the increase in performance would be more than worth any hassle to set it up. Nothing wrong with learning how to do it the file way, so long as you're aware that what you end up with will likely be a Frankenstein (and a very slow one at that) compared with doing it in a database. Commented Jun 9, 2019 at 21:15
  • 8 million is nothing to a database Commented Jun 9, 2019 at 21:25
  • thank you boys for the info!! I'll search info of how to implement a batch insertion Commented Jun 10, 2019 at 7:27

2 Answers 2

1

Another option is that, after we would file_get_content or read our files, we would use a simple expression and collect our numbers using a preg_match_all:

$re = '/([0-9]+)/m';
$str = '1      2      3      4      5      6
7      8      9      10     11     12
1      2      3      4      5      6
1      2      3      4      5      6
';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

foreach ($matches as $key => $numbers) {
    foreach ($numbers as $key2 => $number) {
        echo $number . "\n";
    }
}

var_dump($matches);

array(24) {
  [0]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "1"
  }
  [1]=>
  array(2) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "2"
  }
  [2]=>
  array(2) {
    [0]=>
    string(1) "3"
    [1]=>
    string(1) "3"
  }
  [3]=>
  array(2) {
    [0]=>
    string(1) "4"
    [1]=>
    string(1) "4"
  }
  [4]=>
  array(2) {
    [0]=>
    string(1) "5"
    [1]=>
    string(1) "5"
  }
  [5]=>
  array(2) {
    [0]=>
    string(1) "6"
    [1]=>
    string(1) "6"
  }
  [6]=>
  array(2) {
    [0]=>
    string(1) "7"
    [1]=>
    string(1) "7"
  }
  [7]=>
  array(2) {
    [0]=>
    string(1) "8"
    [1]=>
    string(1) "8"
  }
  [8]=>
  array(2) {
    [0]=>
    string(1) "9"
    [1]=>
    string(1) "9"
  }
  [9]=>
  array(2) {
    [0]=>
    string(2) "10"
    [1]=>
    string(2) "10"
  }
  [10]=>
  array(2) {
    [0]=>
    string(2) "11"
    [1]=>
    string(2) "11"
  }
  [11]=>
  array(2) {
    [0]=>
    string(2) "12"
    [1]=>
    string(2) "12"
  }
  [12]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "1"
  }
  [13]=>
  array(2) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "2"
  }
  [14]=>
  array(2) {
    [0]=>
    string(1) "3"
    [1]=>
    string(1) "3"
  }
  [15]=>
  array(2) {
    [0]=>
    string(1) "4"
    [1]=>
    string(1) "4"
  }
  [16]=>
  array(2) {
    [0]=>
    string(1) "5"
    [1]=>
    string(1) "5"
  }
  [17]=>
  array(2) {
    [0]=>
    string(1) "6"
    [1]=>
    string(1) "6"
  }
  [18]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "1"
  }
  [19]=>
  array(2) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "2"
  }
  [20]=>
  array(2) {
    [0]=>
    string(1) "3"
    [1]=>
    string(1) "3"
  }
  [21]=>
  array(2) {
    [0]=>
    string(1) "4"
    [1]=>
    string(1) "4"
  }
  [22]=>
  array(2) {
    [0]=>
    string(1) "5"
    [1]=>
    string(1) "5"
  }
  [23]=>
  array(2) {
    [0]=>
    string(1) "6"
    [1]=>
    string(1) "6"
  }
}

Demo

Sign up to request clarification or add additional context in comments.

5 Comments

No offense, but regex seems a little too much for this task IMO. OP didn't even mention this as a possibility, which might overwhelm them.
uooooh thank you!! I dont know very much about regular expressions, but it can help me to access to elements. One question, why is created an array in each position with two same elements? [0]=>array(2) {[0]=>string(1) "1" [1]=> string(1) "1"...
another question: using your code I have problem with numbers like this 0.11122212333344, how must be the regular expression for that kind of numbers? Thnk u
$re = '([0-9.]+)m'; it's fine!!
Thank you, as soon as possible I'll post my solution...thnak you back again for your help
0

Making the following assumptions:

  • each file has a string of numbers separated by spaces
  • numbers are not bigger than a billion

code to extract the first element of each file, starting with the code you've provided:

$myfile = fopen("textoPrueba.txt", "r", "/home/berni/Documentos/Vesta1");

// get the first 10 elements one at a time
$str = array();
for($i=0; $i<10; $i++) {
  // get the first 10 elements one at a time
  $str[] = fgetc($myfile);
}
fclose($myfile);

// squish them into a single string
$temp = join($str);

// explode it into an array separated by spaces
$split = explode(' ', $temp);

// get the first number
$first_element_of_file = $split[0];

For learning purposes, this will do what you asked. I make no claims about it being the best way to approach it!

1 Comment

Thank you Tom!! so, for using 8 files I'll have to implement the same code you provided, but mention each file inside the for(), isn't it? I'll try your code when arrive back home, thank you!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.