Deleting from an array
This question already has an answer here:
You could pass $_session into an ArrayIterator and use ArrayIterator::offsetUnset().
For example:-
session_start();
$_SESSION['test1'] = 'Test1';
$_SESSION['test2'] = 'Test2';
$_SESSION['test3'] = 'Test3';
var_dump($_SESSION);
$iterator = new ArrayIterator($_SESSION);
$iterator->offsetUnset('test2');
$_SESSION = $iterator->getArrayCopy();
var_dump($_SESSION);
Output:-
array (size=3)
'test1' => string 'Test1' (length=5)
'test2' => string 'Test2' (length=5)
'test3' => string 'Test3' (length=5)
array (size=2)
'test1' => string 'Test1' (length=5)
'test3' => string 'Test3' (length=5)
This also saves you the expense of looping through the array to find the element you want to delete.
there seems no problem with the way you are doing the delete. But I think the problem is in the structure of the array. For example, string values are not quoted, there are no commas separating array items, and the array keys are written within [].
Try changing your array as below and the delete should work fine:
$_SESSION['Items'] = Array (
0 => Array (
'id' => 18,
'name' => 'book',
'description' => '',
'quantity' => 0,
'price' => 50,
'status' => 'Brand New'
),
1 => Array (
'id' => 19,
'name' => 'testing',
'description' => 'for testing',
'quantity' => 2,
'price' => 182,
'status' => 'Brand New',
),
2 => Array (
'id' => 1,
'name' => 'Fruity Loops',
'description' => 'dj mixer',
'quantity' => 1,
'price' => 200,
'status' => 'Brand New'
)
);
Hope it helps.
链接地址: http://www.djcxy.com/p/30112.html上一篇: 如何只删除数组的项目?
下一篇: 从数组中删除