Reputation: 53
I am trying to delete all the data from within a specific key in my array, but cant work out why my code isn't working. I have used print_r
to check the code works and I can see that the correct value is being printed for each array when I click the 'remove' button, but it isn't removing the data from the key.
My current array looks like this:
Array
(
[0] => Array
(
[0] => S30813-Q100-X303
[1] => 5
[2] => Refurbished
)
[1] => Array
(
[0] => JX-1T1-LTU
[1] => 8
[2] => New
)
)
I am outputting the data to a table with:
for ($row = 0; $row < $totalcount; $row++) {
echo "<tr>";
for ($col = 0; $col < 3; $col++) {
echo "<td>".$contents[$row][$col]."</td>";
}
for ($col = 0; $col < 1; $col++) {
$del = $row;
echo "<td><form action='' method='post'><input type='text' name='del' value=".$row."></input><input type='submit' name='deletepart' value='Remove'></input></form></td>";
}
echo "</tr>";
}
My php to unset the array key (and where I am guessing the problem lies) is:
<?php
if (isset($_POST['deletepart'])) {
$value_to_delete = $_POST['del'];
$value_to_delete = $key;
unset($_SESSION['arr'][$key]);
$_SESSION["arr"] = array_values($_SESSION["arr"]);
}
?>
Any help on where I am going wrong here would be very much appreciated!
Upvotes: 0
Views: 43
Reputation: 7161
try this
write $key = $value_to_delete;
instead of $value_to_delete = $key
<?php
if (isset($_POST['deletepart'])) {
$value_to_delete = $_POST['del'];
//$value_to_delete = $key; //instead of this use
$key = $value_to_delete;
unset($_SESSION['arr'][$key]);
$_SESSION["arr"] = array_values($_SESSION["arr"]);
}
?>
Upvotes: 1