-2

I'm trying to clean my db

if($var == true){
    $db = file('db.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    shuffle($db);
    $db2 = file('db2.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $cleaner = array_unique ($db);
    $final = array_diff ($cleaner, $db2);
    file_put_contents('db3.txt', $final);
}

it should put in db3.txt all users with a newline like:

Mike\nRobert\nJhon\nPolly

but it return all users attached between them

  • 1
    Welcome. Please read the help section on how to provide a [mcse]. Also please provide the test of db.txt and db2.txt. Lastly, your title suggests work on a database, however, your question concerns on working with arrays; I suggest you reword your question title. – Tedinoz Dec 29 '18 at 15:26

2 Answers2

0

You could use implode for add the newline

$comma_separated = implode("\n", $final);
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

You can try like this way,

if($var == true){
    $db = file('db.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    shuffle($db);
    $db2 = file('db2.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $cleaner = array_unique ($db);
    $final = array_diff ($cleaner, $db2);
    $final = implode(PHP_EOL, $final_result); // implode it with PHP_EOL
    // put content to file with FILE_APPEND parameter but you can ignore if you wish
    file_put_contents('db3.txt', $final, FILE_APPEND); 
}
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103