Thursday, 30 August 2018

PHP - Delete element from multidimensional array based on value advertisements

This question already has an answer here:

  • Delete element from multidimensional-array based on value 5 answers
foreach ($array_leave_dates as $emp => $leave_type) {
    foreach ($leave_type as $leave_dates) {
       if($leave_type == 'Maternity Leave'){
         unset($array_leave_dates[$leave_type]);
       }
       else{
         echo $leave_dates[$row];
       }
    }
}

Here we can fetch $leave_dates and want to remove or unset leave_type == 'Maternity Leave'. But could'nt. Please help to point out the mistake in my code above.

Have a look at the // comments
foreach ($array_leave_dates as $emp => $leave_type) {
  // you treat $leave_type as array here
  foreach ($leave_type as $leave_dates) {
   // you treat $leave_type as string here
   // doesn't feel right
   if($leave_type == 'Maternity Leave') {
     // you are unsetting with a value
     //unset($array_leave_dates[ --> $leave_type <-- ]);
     // i assume you want to delete the key
     unset($array_leave_dates[$emp]);
   }
   else{
     // $row doesn't seem to exist, looks wrong from here
     echo $leave_dates[$row];
   }
  }
}

0 comments:

Post a Comment