PHP Find Match in Array Key and Sum the Value -
input:
array ( [address check failed, address discrepancy] => 716 [ssn check failed, dob check failed] => 15 [dob check failed] => 139 [no issues] => 189 [dob check failed, address discrepancy] => 51 [dob check failed, address check failed, address discrepancy] => 23 [ssn check failed] => 3 [address discrepancy] => 33 )
i need sum value of key not contain phrase "ssn check failed"
i using function:
function in_array_r($needle, $haystack, $strict = true) { foreach ($haystack $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; }
like this:
foreach ($issues_totals $key => $value){ if (!in_array_r("ssn check failed", $key)){ $total_with_no_issue += $value; } }
where $issues_totals
above array , $total_with_no_issue
value looking for. problem code $total_with_no_issue
returning 1169, total of entire array. want return 1151.
any appreciated!
you can use array_filter
exclude keys contain target string:
$valid = array_filter($array, function ($e) { return strpos($e, 'ssn check failed') === false; }, array_filter_use_key);
and use array_sum
fetch total of rest:
$total_with_no_issue = array_sum($valid);
this returns 1151 not 1159 mentioned, see https://eval.in/845367
Comments
Post a Comment