I'm trying to make a script to remove any empty elements from my array.
However an empty element is in the [0] slot so when I unset the value it deletes my whole array. At least I think that's what's happening, why isn't this working?
<?php
$idfile = file_get_contents("datafile.dat");
$idArray = explode("\n", $idfile);
print_r($idArray);
foreach ($idArray as $key => &$value) {
echo "Key is: ".$key." and value is: ".$value."<br />\n";
if ($value == ""){
echo "Killing value of ".$value."<br />";
unset($value[$key]);
}
$value = str_replace("\n", "", $value);
$value = str_replace("\r", "", $value);
$value = $value.".dat";
}
print_r($idArray);
?>
Here's the output:
Array
(
[0] =>
[1] => test1
[2] => test2
)
Key is: 0 and value is: <br>
Killing value of <br>