0

我希望遍历多维数组并删除“已批准”设置为零的数组。我尝试使用unset其他stackoverflow线程中建议的方法,但无济于事。我还尝试根据此 stackoverflow 链接重新索引数组。

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

任何帮助将不胜感激。

4

1 回答 1

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);
?>
于 2012-09-30T05:53:10.827 回答