I have two REST urls that I'm using. The first one has event profiles and in my code I'm looping through the and searching each of those in the second rest url.
1 Answer
Use array_filter() to keep the elements that match the filtering criteria, rather than unsetting elements during a loop.
foreach ($policyPayloadCopy["EventProfile"] as &$profile) {
if (count($profile["EventRuleIDList"]["EventRuleID"]) > 1) {
$profile["EventRuleIDList"]["EventRuleID"] = array_filter($profile["EventRuleIDList"]["EventRuleID"], function($rule) use ($checkedArr) {
return in_array($rule["Severity"], $checkedArr);
});
}
}
The reference variable &$profile means that the assignment to $profile["EventRuleIDList"]["EventRuleID"] affects the original $policyPayloadCopy array rather than a copy.
foreachinstead offorto loop over array elements.unset($eventRuleID);doesn't unset anything in the array. What's the point of it?unset($policyPayloadCopy["EventProfile"][$counter]["EventRuleIDList"]["EventRuleID"][$index]);is how you unset an array element.array_filter(), to keep the array elements whose severity are in$checkedArr.$a = 1; $b = $a; unset($b);it only unsets$b, not$a.