0

Having following array:

Array
(
    [notifys] => Array
    (
        [0] => Array
        (
            [notifytype_id] => 10
            [notify_total] => 1
        )

        [1] => Array
        (
            [notifytype_id] => 11
            [notify_total] => 1
        )

        [2] => Array
        (
            [notifytype_id] => 14
            [notify_total] => 3
        )

        [3] => Array
        (
            [notifytype_id] => 14
            [notify_total] => 3
        )

        [4] => Array
        (
            [notifytype_id] => 14
            [notify_total] => 2
        )

        [5] => Array
        (
            [notifytype_id] => 14
            [notify_total] => 32
        )

        [6] => Array
        (
            [notifytype_id] => 14
            [notify_total] => 28
        )

        [7] => Array
        (
            [notifytype_id] => 14
            [notify_total] => 3
        )

        [8] => Array
        (
            [notifytype_id] => 14
            [notify_total] => 7
        )

        [9] => Array
        (
            [notifytype_id] => 2
            [notify_total] => 7
        )

        [10] => Array
        (
            [notifytype_id] => 2
            [notify_total] => 15
        )
    )
)

---------------------------------------------------------------------------------------

Need a PHP solution to do an addition, in example, of all values in [notify_total] WHERE [notifytype_id] == 10 OR [notifytype_id] == 2

So, the result in this example would be:

$result = 23

1
  • That's an OR, not an AND Commented Nov 11, 2013 at 7:38

4 Answers 4

1
$total=0;
foreach($myArray["notifys"] as $value)
{
  if($value["notifytype_id"]==10 || $value["notifytype_id"]==2)
  $total+=$value["notify_total"];
}
echo $total;
Sign up to request clarification or add additional context in comments.

Comments

1

try this:

$sum = 0; 
foreach ($array['notifys'] as $index => $data)
{
   if ($data['notifytype_id']==10 or $data['notifytype_id']==2)
   {
      $sum += $data['notify_total'];
   } 
}
print $sum; 

1 Comment

Nope let it be. Since you wrote this answer yourself and didn't copy an existing answer, and this is helpful as well, there is no reason to delete it.
1

Use array_reduce() for that.

PHP >=5.4:

$result = array_reduce($array['notifys'], function($temp, $item)
{
   $temp+=in_array($item['notifytype_id'], [2,10])?$item['notify_total']:0;
   return $temp;
},0);

PHP 5.3:

$result = array_reduce($array['notifys'], function($temp, $item)
{
   $temp+=in_array($item['notifytype_id'], array(2,10))?$item['notify_total']:0;
   return $temp;
},0);

PHP <=5.2:

$result = array_reduce($array['notifys'], create_function('$temp, $item', '
{
   $temp+=in_array($item["notifytype_id"], array(2,10))?$item["notify_total"]:0;
   return $temp;
}'),0);

4 Comments

Getting a white page with this function. You know why? And what's the advantage of using array_reduce()?
That depends of PHP version you using. Code above is for PHP >=5.4 (in 5.3 there are no short-array definition and in 5.2 or lower there are no closures via function definition)
Using PHP 5.3.3, Sorry :)
See my edit then (but really - upgrade your PHP. There's no reason to stay on ancient versions, and, if it's about hosting - just change it)
0
/*
$arr - array to be searched in
$k   - allowed key to be compared with
You can specify the keys dynamically that you want to compare against.
*/

function getTotal($arr, $k){
    $temp = 0;                                   //initialization
    foreach($arr['notifys'] as $a){
        if( in_array($a['notifytype_id'], $k))
            $temp = $temp + $a['notify_total'];
    }
    return $temp;
}

//Hanky reminded me
$keys = array(2, 10);                            //for this case
echo getTotal($value, $keys);

3 Comments

Forgot to initialize $keys?
No. Actually, Its upto user. So, i have didnt initialized. I have purposefully made dynamic way for that. BTW, thanks for efforts.
A dynamic solution always wins!! Thanks!

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.