从多维数组中取消设置数组

时间:2012-09-29 18:48:40

标签: php arrays for-loop multidimensional-array key

我希望遍历多维数组并删除已将“approved”设置为零的数组。我尝试使用其他stackoverflow线程中建议的unset方法,但无济于事。我还尝试按this stackoverflow link重新索引数组。

foreach ($dentists as $key => $dentists_index)
    {
        if($dentists_index["approved"] == 0)
        {
            unset($dentists[$key]);
        }
    }

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

实际上你的代码看起来是正确的!看看这个效果很好的快速测试并输出一行来调试每个牙医:

<?
$dentists = array(
    array(
        'name' => 'kevin',
        'approved' => 0,
    ),
    array(
        'name' => 'cathy',
        'approved' => 0,
    ),
    array(
        'name' => 'steven',
        'approved' => 1,
    ),
);

foreach ($dentists as $key => $dentists_index) {
    print "Dentist #$key - Approved: " . $dentists_index['approved'] . "<br />\n";

  if($dentists_index['approved'] == 0) {
    unset($dentists[$key]);
  }
}

var_dump($dentists);
?>