PHP:在多维数组中添加值,其中键等于和等于

时间:2013-11-11 07:34:22

标签: php arrays multidimensional-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
        )
    )
)

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

需要PHP解决方案对[notify_total] WHERE [notifytype_id] == 10 [notifytype_id] == 2中的所有值进行添加

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

$ result = 23

4 个答案:

答案 0 :(得分:1)

$total=0;
foreach($myArray["notifys"] as $value)
{
  if($value["notifytype_id"]==10 || $value["notifytype_id"]==2)
  $total+=$value["notify_total"];
}
echo $total;

答案 1 :(得分: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; 

答案 2 :(得分: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);

答案 3 :(得分: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);