0

具有以下数组:

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
        )
    )
)

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

例如,需要一个 PHP 解决方案来添加[notify_total] WHERE [notifytype_id] == 10 OR中的所有值 [notifytype_id] == 2

因此,此示例中的结果将是:

$结果 = 23

4

4 回答 4

1
$total=0;
foreach($myArray["notifys"] as $value)
{
  if($value["notifytype_id"]==10 || $value["notifytype_id"]==2)
  $total+=$value["notify_total"];
}
echo $total;
于 2013-11-11T07:40:03.927 回答
1

尝试这个:

$sum = 0; 
foreach ($array['notifys'] as $index => $data)
{
   if ($data['notifytype_id']==10 or $data['notifytype_id']==2)
   {
      $sum += $data['notify_total'];
   } 
}
print $sum; 
于 2013-11-11T07:41:54.060 回答
1

为此使用array_reduce()

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);
于 2013-11-11T07:45:14.567 回答
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);
于 2013-11-11T07:46:19.403 回答