0

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.

5
  • Things get so much easier if you use foreach instead of for to loop over array elements. Commented Jul 9, 2021 at 20:25
  • unset($eventRuleID); doesn't unset anything in the array. What's the point of it? Commented Jul 9, 2021 at 20:26
  • unset($policyPayloadCopy["EventProfile"][$counter]["EventRuleIDList"]["EventRuleID"][$index]); is how you unset an array element. Commented Jul 9, 2021 at 20:30
  • A better way to do this is with array_filter(), to keep the array elements whose severity are in $checkedArr. Commented Jul 9, 2021 at 20:31
  • If you do $a = 1; $b = $a; unset($b); it only unsets $b, not $a. Commented Jul 9, 2021 at 20:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.