1
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.

devpro
16.1k3 gold badges29 silver badges39 bronze badges
asked Apr 21, 2017 at 15:56
1
  • Show example data of the first array! $array_leave_dates Whitout knowing more try unset($array_leave_dates[$emp]); Note: From where comes $row? Commented Apr 21, 2017 at 16:00

3 Answers 3

1

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];
 }
 }
}
answered Apr 21, 2017 at 16:09
Sign up to request clarification or add additional context in comments.

Comments

1

Not sure what your data source looks like:

<?php
// Remove the whole employee 
$employees = array(
 'emp1' => array('sick' => 'Mon - Tue'),
 'emp2' => array('bun in oven' => '2016 - 2017'),
 'emp3' => array('broken heart' => '2017 - ∞'),
);
foreach ($employees as $emp => $leave) { 
 foreach ($leave as $leaveName => $date) { 
 if($leaveName == 'bun in oven') { 
 unset($employees[$emp]); 
 }
 }
}
print_r($employees);
// OR remove only 'Maternity' from the employee but keep everything else
<?php
$employees = array(
 'emp1' => array('sick' => 'Mon - Tue', 'its friday and im not coming in' => 'Fri'),
 'emp2' => array('bun in oven' => '2016 - 2017', 'sick' => 'Thu'),
 'emp3' => array('broken heart' => '2017 - ∞'),
);
foreach ($employees as $emp => $leave) { 
 foreach ($leave as $leaveName => $date) { 
 if($leaveName == 'bun in oven') { 
 unset($employees[$emp][$leaveName]); 
 }
 }
}
print_r($employees);
answered Apr 21, 2017 at 16:08

Comments

0

If $leave_type can be "Maternity Leave", then why do you search for $leave_dates inside that string value? The question is naturally rethorical. From the very fact that you have two foreach cycles and the second is embedded into the first makes me think that $leave_type is not what you think it is. So, I think you have a multi dimensional array, where the outer keys are employee names or ids and the inner keys are the types. Example:

array(
 'John Doe' => array('Sickness' => array('a', 'b', 'c')),
 'Mary Doe' => array('Sickness' => array('a', 'b', 'c'), 'Maternal Leave' => array('d', 'e'))
)

If that is the case, then you need to modify your cycles:

foreach ($array_leave_dates as $emp => $leave) {
 if ($leave['Maternity Leave']) {
 unset($array_leave_dates[$emp]['Maternity Leave']);
 }
}

If you want a more general solution, then this might help you:

function removeByKey(&$arr, $k) {
 if (arr[$k] !== null) {
 unset arr[$k];
 }
 foreach($arr as $key => $value) {
 if (is_array($value)) {
 $arr[$key] = removeByKey($arr[$key], $k);
 }
 }
 return $arr;
}
answered Apr 21, 2017 at 16:45

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.