PHP如何删除数组中的数组?
我有这样一个数组:PHP如何删除数组中的数组?,php,arrays,Php,Arrays,我有这样一个数组: $_SESSION = Array ( [chose_image] => Array ( [56915e7c48177e251e1c16f1] => Array ( [title] => title1 [author] => author1 [watermark] => watermark1
$_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执行此操作非常重要,因为数组是由用户动态填充的。有可能吗?您可以尝试以下操作:
unset($\u SESSION['selected\u image'][$id])
或$\u会话['selected\u image'][$id]=null代码>尝试以下方法:
$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
});
这就是为unset()创建的unset()
,unset将通过两种方式删除索引。。一个是未设置的,另一个是创建新数组并忽略此数组