I have a text file with the following arrays:
a:3:{i:0;s:5:"class";i:1;s:7:"class01";i:2;s:7:"fa-user";}
a:3:{i:0;s:5:"class";i:1;s:7:"class02";i:2;s:12:"fa-briefcase";}
a:2:{i:0;s:8:"homeText";i:1;s:13:"Battlestation";}
a:3:{i:0;s:5:"class";i:1;s:7:"class03";i:2;s:6:"fa-eye";}
a:3:{i:0;s:5:"class";i:1;s:7:"class04";i:2;s:7:"fa-code";}
a:2:{i:0;s:8:"homeText";i:1;s:7:"Welcome";}
I have the code to add the new homeText array working, but I need to delete the previous one before I add it, so that there's only one at a time. What I have is this but it's not working:
function removePreviousHomeText() {
$fileOptions = 'options.txt';
$file1 = fopen($fileOptions, "a+");
$array = file($fileOptions);
print_r($array);
foreach($array as $subArray){
if ($subArray[0] == 'homeText'){
unset($subArray);
}
}
echo '<p>end result:</p>';
print_r($array);
}
I'd like some help please. There might be a way to override the previous homeText array upon entering the new one, but I'll need this sort of function later on in this project for other similar problems.
UPDATE: I got it to work. This is how I did it:
function removePreviousHomeText() {
$fileOptions = 'options.txt';
$file1 = fopen($fileOptions, "a+");
$array = file($fileOptions);
foreach($array as $key => $value) {
$subArray = unserialize($value);
if ($subArray[0] == 'homeText') {
unset($array[$key]);
}
}
file_put_contents($fileOptions, $array);
}
unserializeto get the actual array in a more readable format.