For example i have an array like this:
$test= array("0" => "412", "1" => "2");
I would like to delete the element if its = 2
$delete=2;
for($j=0;$j<$dbj;$j++) {
if (in_array($delete, $test)) {
unset($test[$j]);
}
}
print_r($test);
But with this, unfortunatelly the array will empty…
How can i delete an exact element from the array?
Thank you
In the loop you’re running the test condition is true because $delete exists in the array. So in each iteration its deleting the current element until
$deleteno longer exists in$test. Try this instead. It runs through the elements of the array (assuming$dbjis the number of elements in$delete) and if that element equals$deleteit removes it.