如果key包含,则从多维数组中删除数组

时间:2014-01-15 20:33:38

标签: php arrays

我有以下多维数组:

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 个答案:

答案 0 :(得分: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');