2014-01-15 138 views
-1

我有以下多維數組:從多維數組中刪除數組,如果鍵包含

Array 
(
    [2014-01-21|699] => Array 
     (
      [id] => 331 
      [datum] => 2014-01-21 
      [title] => workshop 1 
      [alias] => workshop news 
     ) 

    [1970-01-01|507] => Array 
     (
      [id] => 331 
      [datum] => 1970-01-01 
      [title] => workshop 2 
      [alias] => workshop news 
     ) 

[1970-01-01|780] => Array 
     (
      [id] => 331 
      [datum] => 1970-01-01 
      [title] => workshop 3 
      [alias] => workshop news 
     ) 
) 

我怎樣才能去除鍵包含1970-01-01該多維數組看起來像這樣每個陣列:

Array 
    (
     [2014-01-21|699] => Array 
      (
       [id] => 331 
       [datum] => 2014-01-21 
       [title] => workshop 1 
       [alias] => workshop news 
      ) 
) 

謝謝!

託尼

回答

1

可以使用的foreach環和unset()蠻力方法:

foreach ($yourArray as $key => $data) { 
    if ($data['datum'] == '1970-01-01') { 
     unset($yourArray[$key]); 
    } 
} 

或者,您可以使用array_filter()有一個可重複使用的回調,可以進行必要的檢查爲你:

function checkDate($record) { 
    return ($record['datum'] != '1970-01-01'); 
} 

$filteredArray = array_filter($yourArray, 'checkDate'); 
+0

作品!謝謝! –