Php-mysql Course

A PHP function that can be used to merge multiple files, line by line. The function (named mergeLineFiles() ) returns an array with the merged lines; for example an array like this:
array (
  0 => 'line_1 from file1',
  1 => 'line_1 from file2',
  2 => 'line_1 from file3',
  3 => 'line_2 from file1',
  4 => 'line_2 from file2',
  5 => 'line_2 from file3',
  6 => 'line_3 from file1',
  7 => 'line_3 from file2',
  8 => 'line_3 from file3',
  // and so on ...
);
- With implode(), and file_put_contents(), the lines can be saved in another file.

The mergeLineFiles() function receives two parameters: an array with the files (path and name), and the number of lines that will be merged from each file. The second parameter is optional, if it is not added, or it is 0, the function will merge all the lines.

Code of the mergeLineFiles() function

/*
 Merge Multiple Files, Line by Line
 From: https://coursesweb.net/php-mysql/
 - $files = array with files (path and name)
 - $maxlines = maximum number of lines added from each file (0 will add all the lines)

 Returns an array with the merged lines
*/
function mergeLineFiles($files, $maxlines = 0) {
  $final_lines = array();         // stores the merged lines

  // gets and stores files data, arrays with lines of each file
  $filesdata = array();
  $nrfiles = count($files);
  $arr_nr_lines = array();         // stores the number of lines in each file
  for($i=0; $i<$nrfiles; $i++) {
    if(file_exists($files[$i])) {
      $filesdata[] = file($files[$i], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
      $arr_nr_lines[] = count(end($filesdata));
    }
  }

  $nrfiles = count($filesdata);        // number of arrays with lines
  if($nrfiles > 0) {
    $lines = ($maxlines == 0) ? max($arr_nr_lines) : $maxlines;
    for($i=0; $i<$lines; $i++) {
      // traverses the array with file-lines
      // adds in $final_lines the first line of each file, and remove the line from array
      for($i2=0; $i2<$nrfiles; $i2++) {
        if(isset($filesdata[$i2][0])) $final_lines[] = array_shift($filesdata[$i2]);
      }
    }
  }

  return $final_lines;
}
- Example usage, merge the lines of 3 files into another file. It uses the mergeLineFiles() function to get the array with the merged lines, than save them on server with file_put_contents():
<?php
/*
 Merge Multiple Files, Line by Line
 From: https://coursesweb.net/php-mysql/
 - $files = array with files (path and name)
 - $maxlines = maximum number of lines added from each file (0 will add all the lines)

 Returns an array with the merged lines
*/
function mergeLineFiles($files, $maxlines = 0) {
  $final_lines = array();         // stores the merged lines

  // gets and stores files data, arrays with lines of each file
  $filesdata = array();
  $nrfiles = count($files);
  $arr_nr_lines = array();         // stores the number of lines in each file
  for($i=0; $i<$nrfiles; $i++) {
    if(file_exists($files[$i])) {
      $filesdata[] = file($files[$i], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
      $arr_nr_lines[] = count(end($filesdata));
    }
  }

  $nrfiles = count($filesdata);        // number of arrays with lines
  if($nrfiles > 0) {
    $lines = ($maxlines == 0) ? max($arr_nr_lines) : $maxlines;
    for($i=0; $i<$lines; $i++) {
      // traverses the array with file-lines
      // adds in $final_lines the first line of each file, and remove the line from array
      for($i2=0; $i2<$nrfiles; $i2++) {
        if(isset($filesdata[$i2][0])) $final_lines[] = array_shift($filesdata[$i2]);
      }
    }
  }

  return $final_lines;
}

  /* Usage mergeLineFiles() */

$files = array('file1.txt', 'file2.txt', 'file3.txt');      // array with the files
$final_file = 'somefile.txt';           // the file in wich the merged lines wil be saved

// calls the mergeLineFiles() function, and stores all the merged lines
$final_lines = mergeLineFiles($files);

// Or, merge only the first four lines of each files
// $final_lines = mergeLineFiles($files, 4);

// saves the merged files into $final_file
if(file_put_contents($final_file, implode(PHP_EOL, $final_lines))) echo 'Lines saved';
else echo 'Unable to save data';
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is a block element?
<div> <img> <span>
<div>Web Programming and Development</div>
Which CSS code displays the text underlined?
font-style: italic; text-decoration: underline; font-weight: 500;
h2 {
  text-decoration: underline;
}
Click on the JavaScript function that can access other function after a specified time.
insertBefore() setTimeout() querySelector()
function someFunction() { alert("CoursesWeb.net"); }
setTimeout("someFunction()", 2000);
Click on the instruction that returns the number of items of a multidimensional array in PHP.
count($array) count($array, 1) strlen()
$food =["fruits" =>["banana", "apple"), "veggie" =>["collard", "pea"));
$nr_food = count($food, 1);
echo $nr_food;       // 6
Merge Multiple Files, Line by Line

Last accessed pages

  1. jQuery parent, children and nth-child() (13873)
  2. RegExp - Regular Expressions in ActionScript (4713)
  3. JavaScript strip_tags and stripslashes (8829)
  4. Complex Shapes with a single DIV and CSS (3711)
  5. Detecting events in ActionScript 3 (1412)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (643)
  2. The Mastery of Love (75)
  3. CSS cursor property - Custom Cursors (71)
  4. Read Excel file data in PHP - PhpExcelReader (67)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (48)