I have a problem with the unset of my session because I've done it on a function and only the internal parameter delete the select value id.
Have a look:
/***
* @name DeleteItem
* @date 04.10.2014
* @param The array that contains the element to delete
* @param The id of the selected index
* @param The name of the button that start the delete
* @version 1.0
* @description Delete an item to the select
*/
function DeleteItem($array,$SelectID,$btnDelete) {
//The delete button was clicked?
if(isset($_POST[$btnDelete]))
{
//Find the element with the id and delete the value
foreach ($array as $idx =>$value)
{
if ($idx == $SelectID)
{
unset($array,$SelectID);
}
}
return $array;
}
Thanks - I'm sure that it's something really simple to do.
Finally found was just a question of references values(i was working whith copy) I just add a & before function
Your unset()
syntax is wrong for the type of operation you are trying to do. You want to delete only index $SelectID
from array. Try below code:
unset($array[$SelectID]);
Also, you don't need the loop. Below is a simplified version:
function DeleteItem($array,$SelectID,$btnDelete) {
//The delete button was clicked? and if index exists in array
if(isset($_POST[$btnDelete]) && array_key_exists($SelectID, $array)) {
unset($array[$SelectID]);
}
return $array;
}
And, you need to delete (call DeleteItem()
) only if the POST variable exists. So, you can further simplify as below and remove isset($_POST[$btnDelete])
from if
condition:
if(isset($_POST[$btnDelete])) {
DeleteItem($array,$SelectID);
}
You are using 'unset' incorrectly.
If $SelectID
is a value of the array,
$index = array_search($SelectID, $array);
if ($index) {
unset($array[$index]);
}
or, if the $SelectID
is 'key' of the array & not a value, then...
$keys = array_keys($array);
$index = array_search($SelectID, $keys);
if (!empty($keys[$index])) {
unset($array[$keys[$index]]);
}
print_r($array);
(Pss: we don't need a foreach)