Creating an array reading from different files in PHP

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)

Upvotes: 0

Views: 355

Answers (2)

Emma
Emma

Reputation: 27723

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

Upvotes: 1

Tim Morton
Tim Morton

Reputation: 2644

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!

Upvotes: 0

Related Questions