unemployment Posted February 3, 2012 Share Posted February 3, 2012 Why won't my unset remove the array from the parent $data[0] array? foreach ($data[0] as $k => $r) { if (($r['feedid'] !== $user_info['uid']) && ($r['action_id'] == 'newuser')) { unset($data[0][$k]); } } Link to comment https://forums.phpfreaks.com/topic/256309-unset-child-wont-remove-from-parent/ Share on other sites More sharing options...
scootstah Posted February 3, 2012 Share Posted February 3, 2012 The condition isn't met? Link to comment https://forums.phpfreaks.com/topic/256309-unset-child-wont-remove-from-parent/#findComment-1313923 Share on other sites More sharing options...
Psycho Posted February 3, 2012 Share Posted February 3, 2012 My guess would be that the condition is never true. Without examining the data there's no way to be sure. But, you can add some debugging code to check for yourself foreach ($data[0] as $k => $r) { //Debugging code: echo ($r['feedid'] !== $user_info['uid']) ? "'{$r['feedid']}' !== '{$user_info['uid']}'" : "'{$r['feedid']}' === '{$user_info['uid']}'"; echo " : "; echo ($r['action_id'] == 'newuser') ? "'{$r['action_id']}' == 'newuser'" : "'{$r['action_id']}' != 'newuser'"; echo (($r['feedid'] !== $user_info['uid']) && ($r['action_id'] == 'newuser')) ? "TRUE" : "FALSE"; echo "<br>\n"; if (($r['feedid'] !== $user_info['uid']) && ($r['action_id'] == 'newuser')) { unset($data[0][$k]); } } If the results still don't look right try using var_dump() on the first two values since they must be the same value AND the same type. Link to comment https://forums.phpfreaks.com/topic/256309-unset-child-wont-remove-from-parent/#findComment-1313924 Share on other sites More sharing options...
scootstah Posted February 3, 2012 Share Posted February 3, 2012 My guess would be that the condition is never true. Without examining the data there's no way to be sure. But, you can add some debugging code to check for yourself foreach ($data[0] as $k => $r) { //Debugging code: echo ($r['feedid'] !== $user_info['uid']) ? "'{$r['feedid']}' !== '{$user_info['uid']}'" : "'{$r['feedid']}' === '{$user_info['uid']}'"; echo " : "; echo ($r['action_id'] == 'newuser') ? "'{$r['action_id']}' == 'newuser'" : "'{$r['action_id']}' != 'newuser'"; echo (($r['feedid'] !== $user_info['uid']) && ($r['action_id'] == 'newuser')) ? "TRUE" : "FALSE"; echo "<br>\n"; if (($r['feedid'] !== $user_info['uid']) && ($r['action_id'] == 'newuser')) { unset($data[0][$k]); } } If the results still don't look right try using var_dump() on the first two values since they must be the same value AND the same type. *rubs eyes* Yup, time for bed. Link to comment https://forums.phpfreaks.com/topic/256309-unset-child-wont-remove-from-parent/#findComment-1313925 Share on other sites More sharing options...
unemployment Posted February 3, 2012 Author Share Posted February 3, 2012 My guess would be that the condition is never true. Without examining the data there's no way to be sure. But, you can add some debugging code to check for yourself foreach ($data[0] as $k => $r) { //Debugging code: echo ($r['feedid'] !== $user_info['uid']) ? "'{$r['feedid']}' !== '{$user_info['uid']}'" : "'{$r['feedid']}' === '{$user_info['uid']}'"; echo " : "; echo ($r['action_id'] == 'newuser') ? "'{$r['action_id']}' == 'newuser'" : "'{$r['action_id']}' != 'newuser'"; echo (($r['feedid'] !== $user_info['uid']) && ($r['action_id'] == 'newuser')) ? "TRUE" : "FALSE"; echo "<br>\n"; if (($r['feedid'] !== $user_info['uid']) && ($r['action_id'] == 'newuser')) { unset($data[0][$k]); } } If the results still don't look right try using var_dump() on the first two values since they must be the same value AND the same type. Thanks for the code. The condition is being met. I think the problem is just that the loop continues to run through all the variables even after the array is unset and still adds in content. Is there anyway to tell the foreach to go to the next $k value after I unset the array? Link to comment https://forums.phpfreaks.com/topic/256309-unset-child-wont-remove-from-parent/#findComment-1313928 Share on other sites More sharing options...
Psycho Posted February 3, 2012 Share Posted February 3, 2012 Thanks for the code. The condition is being met. I think the problem is just that the loop continues to run through all the variables even after the array is unset and still adds in content. Is there anyway to tell the foreach to go to the next $k value after I unset the array? Not sure what you mean. The loop WILL continue to the next value on each iteration. Based upon the code you posted (assuming sequential, numerically based indexes) if the values for key ($K) 3 meet the criteria, $data[0][3] will be unset and the loop will start over with $k being equal to 4. I think you are leaving out some pertinent information. however, I will add that I think you might want to look into using array_filter() with a custom function. Link to comment https://forums.phpfreaks.com/topic/256309-unset-child-wont-remove-from-parent/#findComment-1313932 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.