How can I add values of key 2 of inner arrays for the similar values of key 0 of inner arrays.
Thanks for your help i’m stuck on this problem. Let me explain it, i have this kind of array. The value of key0 in first 3 arrays is same i.e. 10002, so sum key2 of those 3 arrays.
array (size=6) 0 => array (size=4) 0 => float 10002 1 => string 'xxxyyyy' (length=7) 2 => float 5.00 3 => float 0 1 => array (size=4) 0 => float 10002 1 => string 'xxxyyyy' (length=7) 2 => float 5.00 3 => float 4.25 2 => array (size=4) 0 => float 10002 1 => string 'xxxyyyy' (length=7) 2 => float 5.00 3 => float 0 3 => array (size=4) 0 => float 10010 1 => string 'yyyyyyy' (length=7) 2 => float 10.00 3 => float 0 4 => array (size=4) 0 => float 10010 1 => string 'yyyyyyy' (length=7) 2 => float 10.00 3 => float 1.85 5 => array (size=4) 0 => float 10010 1 => string 'yyyyyyy' (length=7) 2 => float 10.00 3 => float 0
Is it possible to get this result (if key0 values are same of inner arrays then sum key2 values )
sum_key_2 = 15, for key_0 = 10002 //key 0 value is similar in first 3 arrays so add key 2 values of those 3 arrays sum_key_2 = 30, for key_0 = 10010
I tried this but not getting desired output:
$ bill = 0; $ sp_no = array(); foreach($ data as $ innerKey => $ innerArray) { $ sp_no[] = $ innerArray[0]; } foreach($ data as $ k => $ val) { if ($ sp_no[$ k] == $ val[0]) { $ bill += $ val[2]; } } echo $ bill;