我想在php中获取多维数组中两个数组键的总和

时间:2018-03-23 10:59:50

标签: php arrays multidimensional-array

我有这个数组,其中一些学科名称是相同的,我想得到相同学科名称的crypt_count的总和。

Array
(
 [0] => Array
    (
        [discipline_name] => Fortitude
        [library_count] => 0
        [crypt_count] => 3
    )

[1] => Array
    (
        [discipline_name] => Fortitude
        [library_count] => 0
        [crypt_count] => 1
    )

[2] => Array
    (
        [discipline_name] => Obtenebration
        [library_count] => 0
        [crypt_count] => 3
    )

[3] => Array
    (
        [discipline_name] => Obtenebration
        [library_count] => 0
        [crypt_count] => 2
    )

[4] => Array
    (
        [discipline_name] => Dementation
        [library_count] => 1
        [crypt_count] => 0
    )

[5] => Array
    (
        [discipline_name] => Obfuscate
        [library_count] => 0
        [crypt_count] => 2
    )

[6] => Array
    (
        [discipline_name] => Fortitude
        [library_count] => 0
        [crypt_count] => 3
    )

[7] => Array
    (
        [discipline_name] => Necromancy
        [library_count] => 0
        [crypt_count] => 1
    )

[8] => Array
    (
        [discipline_name] => Necromancy
        [library_count] => 0
        [crypt_count] => 1
    )

[9] => Array
    (
        [discipline_name] => Celerity
        [library_count] => 0
        [crypt_count] => 1
    )

[10] => Array
    (
        [discipline_name] => Redemption
        [library_count] => 1
        [crypt_count] => 0
    )

[11] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 1
        [crypt_count] => 0
    )

[12] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 6
        [crypt_count] => 0
    )

[13] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 1
        [crypt_count] => 0
    )

[14] => Array
    (
        [discipline_name] => Obtenebration
        [library_count] => 4
        [crypt_count] => 0
    )

[15] => Array
    (
        [discipline_name] => Necromancy
        [library_count] => 4
        [crypt_count] => 0
    )

[16] => Array
    (
        [discipline_name] => Necromancy
        [library_count] => 2
        [crypt_count] => 0
    )

[17] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 1
        [crypt_count] => 0
    )

[18] => Array
    (
        [discipline_name] => Necromancy
        [library_count] => 1
        [crypt_count] => 0
    )

[19] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 3
        [crypt_count] => 0
    )

[20] => Array
    (
        [discipline_name] => Obtenebration
        [library_count] => 2
        [crypt_count] => 0
    )

[21] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 1
        [crypt_count] => 0
    )

[22] => Array
    (
        [discipline_name] => Auspex
        [library_count] => 1
        [crypt_count] => 0
    )

[23] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 4
        [crypt_count] => 0
    )

[24] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 1
        [crypt_count] => 0
    )

[25] => Array
    (
        [discipline_name] => Potence
        [library_count] => 1
        [crypt_count] => 0
    )

[26] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 1
        [crypt_count] => 0
    )

[27] => Array
    (
        [discipline_name] => Necromancy
        [library_count] => 2
        [crypt_count] => 0
    )

[28] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 0
        [crypt_count] => 1
    )

[29] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 0
        [crypt_count] => 1
    )

[30] => Array
    (
        [discipline_name] => Obtenebration
        [library_count] => 0
        [crypt_count] => 4
    )

[31] => Array
    (
        [discipline_name] => Obtenebration
        [library_count] => 0
        [crypt_count] => 2
    )

[32] => Array
    (
        [discipline_name] => Potence
        [library_count] => 0
        [crypt_count] => 1
    )

[33] => Array
    (
        [discipline_name] => Potence
        [library_count] => 0
        [crypt_count] => 1
    )

)

3 个答案:

答案 0 :(得分:2)

尝试使用foreach循环遍历数组并为总计构建最终数组:

$totals = [];
foreach($array as $arr) {
    if (!isset($totals[$arr['discipline_name']]) {
        $totals[$arr['discipline_name']] = 0;
    }
    $totals[$arr['discipline_name']] += $arr['crypt_count'];
}

可能需要针对您的特定用例进行调整/清理,但基本逻辑应该有效。

答案 1 :(得分:1)

我的方法有点不同,理解起来有点复杂,但另一方面它更灵活。

实质上,这会将每个discipline_name的所有数据(或者你抛出的任何未来索引)合并到一个数组中。这样可以轻松处理数据。

// Your array data
$json = '[{"discipline_name":"Fortitude","library_count":"0","crypt_count":"3"},{"discipline_name":"Fortitude","library_count":"0","crypt_count":"1"},{"discipline_name":"Obtenebration","library_count":"0","crypt_count":"3"},{"discipline_name":"Obtenebration","library_count":"0","crypt_count":"2"},{"discipline_name":"Dementation","library_count":"1","crypt_count":"0"},{"discipline_name":"Obfuscate","library_count":"0","crypt_count":"2"},{"discipline_name":"Fortitude","library_count":"0","crypt_count":"3"},{"discipline_name":"Necromancy","library_count":"0","crypt_count":"1"},{"discipline_name":"Necromancy","library_count":"0","crypt_count":"1"},{"discipline_name":"Celerity","library_count":"0","crypt_count":"1"},{"discipline_name":"Redemption","library_count":"1","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"1","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"6","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"1","crypt_count":"0"},{"discipline_name":"Obtenebration","library_count":"4","crypt_count":"0"},{"discipline_name":"Necromancy","library_count":"4","crypt_count":"0"},{"discipline_name":"Necromancy","library_count":"2","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"1","crypt_count":"0"},{"discipline_name":"Necromancy","library_count":"1","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"3","crypt_count":"0"},{"discipline_name":"Obtenebration","library_count":"2","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"1","crypt_count":"0"},{"discipline_name":"Auspex","library_count":"1","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"4","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"1","crypt_count":"0"},{"discipline_name":"Potence","library_count":"1","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"1","crypt_count":"0"},{"discipline_name":"Necromancy","library_count":"2","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"0","crypt_count":"1"},{"discipline_name":"Dominate","library_count":"0","crypt_count":"1"},{"discipline_name":"Obtenebration","library_count":"0","crypt_count":"4"},{"discipline_name":"Obtenebration","library_count":"0","crypt_count":"2"},{"discipline_name":"Potence","library_count":"0","crypt_count":"1"},{"discipline_name":"Potence","library_count":"0","crypt_count":"1"}]';
$data = json_decode($json, true);

// Nifty function to restructure the array
// It is flexible and thus reuseable code, so I recommend defining it someone that is loaded always.
function array_groupby($a, $i, $rem = true) {
  foreach($a as $v){
    $k = $v[$i];
    if($rem){
      unset($v[$i]);
    }
    $t[$k][] = $v;
  }

  return $t;
}

// The actual code.
// Loop over a new array that is grouped by name
foreach(array_groupby($data, 'discipline_name') as $deciple => $values){
  // this will merge the column value of the count to a totals.
  $totals[$deciple] = array_sum(array_column($values,'crypt_count'));
}

print_r($totals);
  
    

Array ( [Fortitude] => 7 [Obtenebration] => 11 [Dementation] => 0 [Obfuscate] => 2 [Necromancy] => 2 [Celerity] => 1 [Redemption] => 0 [Dominate] => 2 [Auspex] => 0 [Potence] => 2 )

  

答案 2 :(得分:-1)

假设你有$array var。让我们声明两个数组$groupedItems,它们通过' discipline_name'来描述不同的项目。和$result这将是最终输出。

$groupedItems = array();
$result = array();


function in_group($item){
    foreach($groupedItems as $i){
        if($item['discipline_name'] === $item['discipline_name'])
            return true;
    }
    return false;
}

function count_crypt($item){
    $c = 0;
    foreach($array as $i){
        if($i['discipline_name'] === $item['discipline_name'])
            $c = $c + $i['crypt_count'];
    }
    return $c;
}

foreach($array as $item){
    if(!in_group($item)){
        array_push($groupedItems, $item);
    }
}


foreach($groupedItems as $item){
    array_push($result, array('discipline_name'=>$item['discipline_name'], 'count_crypt'=>count_crypt($item));
}



var_dump($result);

修改

<?php

function in_group($item, $groupedItems){
    foreach($groupedItems as $i){
        if($item['discipline_name'] === $i['discipline_name'])
            return true;
    }
    return false;
}

function count_crypt($item, $array){
    $c = 0;
    foreach($array as $i){
        if($i['discipline_name'] === $item['discipline_name'])
            $c = $c + $i['crypt_count'];
    }
    return $c;
}

$array = array
(
 array
    (
        'discipline_name' => 'Fortitude',
        'library_count' => 0,
        'crypt_count' => 3,
    ),

 array
    (
        'discipline_name' => 'Fortitude',
        'library_count' => 0,
        'crypt_count' => 1
    ),

 array
    (
        'discipline_name' => 'Obtenebration',
        'library_count' => 0,
        'crypt_count' => 3
    )
);

$groupedItems = array();
$result = array();




foreach($array as $item){

    if(!in_group($item, $groupedItems)){

        array_push($groupedItems, $item);
    }
}


foreach($groupedItems as $item){
    array_push($result, array('discipline_name'=>$item['discipline_name'], 'count_crypt'=>count_crypt($item, $array)));
}



var_dump($result);
?>