企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
行求和 ``` $a=array(0=>5,1=>"15s",2=>25); echo array_sum($a); //45 ``` 列求和 ``` $arr1=[ ['feb'=>5,'dec'=>5,total=>10], ['feb'=>10,'dec'=>10,total=>20], ]; $feb_total= array_sum(array_column($arr, 'feb')); ``` 合并列求和 ``` //已知 $arr1=[ ['feb'=>5,'dec'=>5,total=>10], ['feb'=>10,'dec'=>10,total=>20], ]; $arr2=[ ['feb'=>5,'dec'=>5,total=>10], ['feb'=>10,'dec'=>10,total=>20], ]; //求合并后这样的数据 $arr2=[ ['feb'=>10,'dec'=>10,total=>20], ['feb'=>20,'dec'=>20,total=>40], ]; ``` ``` fieldElementSum($data2,$marketingDepartmentData) function fieldElementSum($arr1,$arr2){ if (empty($arr1)) { return $arr2; }else{ $loop=(count($arr1)-count($arr2)>0)?count($arr1):count($arr2); for ($k=0; $k < $loop; $k++) { $arr1[$k]['jan']=bcadd($arr1[$k]['jan']??0, $arr2[$k]['jan']??0,2); $arr1[$k]['dec']=bcadd($arr1[$k]['dec']??0, $arr2[$k]['dec']??0,2); $arr1[$k]['total']=bcadd($arr1[$k]['total']??0, $arr2[$k]['total']??0,2); } } return $arr1; } ``` 多个一维数组相同key的值相加合并 ``` fieldMergeData(['jian'=>5,'dec'=>5,total=>10],['feb'=>5,'dec'=>5,total=>10]) function fieldMergeData(...$fieldArr){ $tmp=[ "jan" => "0", "dec" => "0", "total" => "0", ]; foreach ($fieldArr as $v) { $tmp['jan']=bcadd($tmp['jan'], $v['jan'],2); $tmp['dec']=bcadd($tmp['dec'], $v['dec'],2); $tmp['total']=bcadd($tmp['total'], $v['total'],2); } return $tmp; } ``` 三个维度的数据合并 ~~~ //三个维度的数据合并 $names = array('zhangsan', 'lisi', 'wangwu'); $sex = array("man", "sex", "man"); $age = array(20, 25, 28); $users = array_map(null, $names, $sex, $age); print_r($users); //输出结果如下: Array ( [0] => Array ( [0] => zhangsan [1] => 20 [2] => man ) [1] => Array ( [0] => lisi [1] => 25 [2] => sex ) [2] => Array ( [0] => wangwu [1] => 28 [2] => man ) ) $a=[ 'jan' => "15.00", 'feb' => "5.00", 'may' => "5.00", ]; $b=[ 'jan' => "15.00", 'feb' => "5.00", 'may' => "5.00", ]; $data = array_map(null, $a,$b); 结果: [ ["15.00","15.00"], ["5.00","5.00"], ["5.00","5.00"] ] ~~~