0

i have an array like this:

$_SESSION = Array ( 
        [chose_image] => Array (
             [56915e7c48177e251e1c16f1] => Array (
                [title] => title1
                [author] => author1 
                [watermark] => watermark1 
                [file_name] => name1.jpg 
                [visible] => 1 
                [_id] => MongoId Object ( [$id] => 56915e7c48177e251e1c16f1 )
             ) 
            [56915dad48177ee21d1c16f0] => Array (
                [title] => title2
                [author] => author2
                [watermark] => watermark2
                [file_name] => name2.jpg
                [visible] => 1
                [_id] => MongoId Object ( [$id] => 56915dad48177ee21d1c16f0 )
            )
        )
    )

And I would like to delete the whole array:

Array (
             [56915e7c48177e251e1c16f1] => Array (
                [title] => title1
                [author] => author1 
                [watermark] => watermark1 
                [file_name] => name1.jpg 
                [visible] => 1 
                [_id] => MongoId Object ( [$id] => 56915e7c48177e251e1c16f1 )

by using the [$id]. It is very important to do this by $id, because the array is dynamically filling by users. Is it possible?

devpro
  • 16,184
  • 3
  • 27
  • 38
SigGP
  • 716
  • 1
  • 11
  • 24

2 Answers2

1

You can try this: unset($_SESSION['chose_image'][$id]); or $_SESSION['chose_image'][$id] = null;

Volodymyr Chumak
  • 756
  • 6
  • 12
1

Try this approach:

$sample_id = "56915e7c48177e251e1c16f1"; // for example
$_SESSION['chose_image'] = array_filter($_SESSION['chose_image'], function($v) use($sample_id){
    return $v["_id"]->$id != $sample_id; // Though $id is bad name for object property
});
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105