PHP怎么删除数组中的数组?

时间:2016-01-09 22:49:02

标签: php arrays

我有一个这样的数组:

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

我想删除整个数组:

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

使用[$ id]。通过$ id执行此操作非常重要,因为数组是由用户动态填充的。有可能吗?

2 个答案:

答案 0 :(得分:1)

您可以尝试:unset($_SESSION['chose_image'][$id]);$_SESSION['chose_image'][$id] = null;

答案 1 :(得分:1)

尝试这种方法:

$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
});