0

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 to sum the value of any key that does not contain the phrase "SSN CHECK FAILED"

I am using this function:

function in_array_r($needle, $haystack, $strict = true) {
foreach ($haystack as $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 as $key => $value){

    if (!in_array_r("SSN CHECK FAILED", $key)){

    $total_with_no_issue += $value;

  }
}

Where $issues_totals is the above array and $total_with_no_issue is the value I am looking for. The problem is that with this code $total_with_no_issue is returning 1169, which is the total of the entire array. I want it to return 1151.

Any help is appreciated!

3 Answers 3

2

You can use array_filter to exclude any keys that contain the target string:

$valid = array_filter($array, function ($e) {
    return strpos($e, 'SSN CHECK FAILED') === false;
}, ARRAY_FILTER_USE_KEY);

and then simply use array_sum to fetch the total of the rest:

$total_with_no_issue = array_sum($valid);

This actually returns 1151 not 1159 as you mentioned, see https://eval.in/845367

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

1 Comment

This is the best, cleanest, most direct answer that uses the most appropriate functions (which for the record can be turned into a one-liner). Using regex is utterly unnecessary. I was going to post this as my answer until I passed it on my way to the textbox. sandbox.onlinephpfunctions.com/code/…
2

I hope this one will be helpful too.

Solution 1: Try this code snippet here

<?php
ini_set('display_errors', 1);
$array=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
);
$requiredKeys=  array_flip(preg_grep('/SSN CHECK FAILED/', array_keys($array),PREG_GREP_INVERT));
$result=0;
foreach($array as $key => $value)
{
    if(isset($requiredKeys[$key]))
    {
        $result+=$value;  
    }
}
echo $result;

Solution 2: Try this code snippet here

Note: This one is non-efficient in terms of performance but will work perfectly fine.

<?php
ini_set('display_errors', 1);
$array=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
);

$array2=  preg_grep('/SSN CHECK FAILED/', array_keys($array),PREG_GREP_INVERT);
print_r(array_sum(array_intersect_key($array, array_flip($array2))));

Comments

0

What you need to do here is two things:

1) Filter the items that their key doesn't contain the phrase you want to exclude:

$filtered = array_filter($arr, function ($item, $key) {
  return !preg_match( '#SSN CHECK FAILED#i', $key);
}, ARRAY_FILTER_USE_BOTH)

2) Sum the filtered items

$sum = array_reduce( $filtered, function ($a, $b){ return $a + $b; })

Note: I'm using anonymous functions and the ARRAY_FILTER_USE_BOTH flag, thought that you might use PHP 5.6+

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.