Adding data instead of overwriting old one. How to limit array to 4 indexes?

Hi :),
I am doing PHP personal form. My problem is based on adding persons.
When I am adding new person, the person is overwriting older one. Plus I would like to get only 4 indexes like below:

Array ( [0] => John [1] => 20 [2] => 999-888-000 [3] => john@gmail.com);

Instead of it I got many indexes.:

Array ( [0] => olaa [1] => 20 [2] => 999-888-000 [3] => ola@gmail.comMegi [4] => 20 [5] => 999-888-000 [6] => megi@gmail.comMegi [7] => 20 [8] => 999-888-000 [9] => megi@gmail.com )

Additionally, index number 3 is connected with name like below:

[3] => ola@gmail.comMegi 

My code source - Source code

You don’t seem to add any kind of line terminator when you append the new data to the file, and I can’t see any note that file_put_contents() will do it for you. I suspect that’s why you find that the email field (the last field in the row) has the name (the first field in the row) appended to it.

You’d add a newline character "\n" at the end of the line when writing to the file; you can then parse the file line by line using fgetcsv() like so:

$contacts = fopen(CONTACT_LIST_FILE, 'r');

while (!feof($contacts)) {
    $data = fgetcsv($contacts);
    print_r($data);
}

(x-post)

Thank you all :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.