簡體   English   中英

PHP:多維數組中鍵等於和等於的值的加法

[英]PHP: Addition of values in multidimensional array where key equal to and equal to

具有以下數組:

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

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

嘗試這個:

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

為此使用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);
/*
$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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM