2016-01-09 39 views
0

我有一個這樣的數組:PHP如何在數組中刪除數組?

$_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做到這一點非常重要,因爲數組是由用戶動態填充的。可能嗎?

+1

這就是'未設置()由'爲 – Jordy

+0

取消設置會刪除THW指數permently – devpro

+0

兩種方式..一個是未設置的第二個是創建一個新的數組並忽略這一個 – devpro

回答

1

你可以試試這個:unset($_SESSION['chose_image'][$id]);$_SESSION['chose_image'][$id] = null;

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