简体   繁体   中英

Unset arrays from multidimensional arrays

I wish to iterate through a multidimensional array and remove arrays that have "approved" set to zero. I have tried using the unset method as suggested in other stackoverflow threads, but to no avail. I have also tried to reindex the array as per this stackoverflow link .

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

Any help would be greatly appreciated.

Actually your code looks correct! Check out this quick test that worked well and outputs a line to debug each dentist:

<?
$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);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM