0

I have this array in which discipline names are same for some of them, I want to get the sum of the crypt_count of same discipline names.

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
    )

)
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
PrashJ
  • 71
  • 2
  • 6
  • 5
    "_I have tried a alot to figure it out, but couldn't succeeded_" Show us what you tried and where your code fails – brombeer Mar 23 '18 at 11:01
  • I have tried to get the number of "Fortitude (discipline names)" in the array, then tried to sum all of its crypt_counts, but couldn't got the result. – PrashJ Mar 23 '18 at 11:04
  • Can anyone explain how can I get the sum of the crypt_counts of the same disciplines? – PrashJ Mar 23 '18 at 11:05

2 Answers2

2

Try iterating over your array with a foreach loop and building a final array for the totals:

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

Might need to be adjusted / cleaned up for your particular use case, but the basic logic should work.

MCMXCII
  • 1,043
  • 4
  • 13
  • 26
1

My approach would be a bit different, its a bit more complicated to understand but at the other hand it is more flexible.

In essence, this merges all the data of every discipline_name (or whatever future index you throw at it) into 1 array. This allows for easy data manipulation.

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

Xorifelse
  • 7,878
  • 1
  • 27
  • 38