0

i have an array that looks like this:

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
    ) 
)

I want to be able to delete an entire row in the array (when a user clicks a delete link) say array[1] which is:

[1] => Array ( 
    [id] => 19 
    [name] => testing   
    [description] => for testing 
    [quantity] => 2 
    [price] => 182 
    [status] => Brand New
)

I have this code where i'm trying to delete based on the id of the product but it doesn't work

//$_SESSION['items'] is the array and $delid is the "product id" gotten when a user clicks  delete on a particular row.
foreach ($_SESSION['Items'] as $key => $products) { 
    if ($products['id'] == $delid) {
        unset($_SESSION['Items'][$key]);
    }
}

How do i implement this? Thanks

Nik
  • 2,885
  • 2
  • 25
  • 25
dhani
  • 127
  • 2
  • 5
  • 9
  • 1
    Could you format that array so it's easier to read please? – vascowhite May 26 '13 at 15:25
  • okay.. i have formatted it – dhani May 26 '13 at 15:30
  • The code looks perfectly fine. Are you sure isn't a typo "items" with "Items"? - in comment u say it's "items". Also, are you sure $delid is fine and u verify if it is in array ? - to not search for an item that isn't in the array. – ioan May 26 '13 at 15:39
  • i wonder why this is downvoted. @GamsterKatalin it is not a typo. But i was able to solve it using 'vascowhite's' answer. – dhani May 26 '13 at 16:31

2 Answers2

2

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.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • ok but i don't know how to use ArrayIterators. I just did a google search and it talks about creating objects, which is a bit confusing for me. How is it used? thanks – dhani May 26 '13 at 15:40
  • I have given you an example, also see the manual page I linked to. http://php.net/manual/en/class.arrayiterator.php – vascowhite May 26 '13 at 15:42
  • ok. but with this how do i get the index of the row to delete, say array[1]? since this depends on the condition `if($delid=='some_id_in_the_array')`?. where $delid is the id of item on which delete was clicked. – dhani May 26 '13 at 16:03
  • okay i got it, though i'm not sure if it's the most efficient way to do it. I still used a for each loop to get the index of the item on which delete was clicked and then applied this iterator class and it worked. – dhani May 26 '13 at 16:28
  • You're right that in this case it's no more efficient, but it works and you learnt something new :). TBH I came up with this answer before you edited your array and didn't inspect it closely :) – vascowhite May 26 '13 at 16:31
1

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.

Abhay
  • 6,545
  • 2
  • 22
  • 17
  • The array is populated as i fetch items from the database in a while loop. The array above is exactly as var_dump($_SESSION['Items']) shows it. thanks – dhani May 26 '13 at 15:46