Hi,
I'm writing the following data to a text file.
"name|price|image|text".
The following code does exactly that.
$filename = 'test.txt';
// Read file in to array. Each element is a line.
$lines = file($filename, FILE_IGNORE_NEW_LINES);
$data_to_add = $somecontent;
// If the new data does not exist in the array
if(!in_array($data_to_add, $lines)) {
unset($lines[11]); // delete the 10th line
array_unshift($lines, $data_to_add); // Put new data at the front of the array
// Write the new array to the file
file_put_contents($filename, implode("\n", $lines));
}
I'm not sure what I ahve to do to make sure that the arrray only searches the first part of the data which in this case is "name" to make sure only unique data is saved?