$x = array();
$x[] = '1';
$x[] = '2';
But have it added permanently to the array?
I have idea on how we could try doing it:
1~2~3;file_get_contents to apply it to a variable $y;explode();x[] = y[0]; x[] = y[1]; x[] = y[2];
But im wondering is that the best way of doing it?
Use serialize on your array, which converts it into a string that you can write to a file
file_put_contents("myfile",serialize($x));
and unserialize to convert the string back into an array
$x = unserialize(file_get_contents("myfile"));
If you only need to append values to the very end of the array, you can use file_put_contents with the FILE_APPEND option, which is faster than reading the entire array into memory and writing it back to disk
file_put_contents("myfile", "1\n", FILE_APPEND);
file_put_contents("myfile", "2\n", FILE_APPEND);
and read the array using file (which uses \n as a delimiter by default)
$x = file("myfile");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With