我需要按键查找数组值,如果其他数组中存在键,则将此值相加。我尝试了不同的组合,但我不是一个好主意。
<link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
<div class="big-div">
<div class="small-div">
<i class="fa fa-user-circle-o" aria-hidden="true"></i>
<p>Number 1</p>
</div>
<div class="small-div">
<i class="fa fa-user-circle-o" aria-hidden="true"></i>
<p>Number 2</p>
</div>
<div class="small-div">
<i class="fa fa-user-circle-o" aria-hidden="true"></i>
<p>Number 3</p>
</div>
<div class="small-div">
<i class="fa fa-user-circle-o" aria-hidden="true"></i>
<p>Number 4</p>
</div>
</div>
我需要比较密钥 - 每个网址的“类别”,“类别2”...如果我在两个网址数组中都有密钥,则需要汇总值。
我尝试在此示例中执行此操作 link to compare array code
答案 0 :(得分:1)
您可以在ne数组中汇总值:
// $arr1 - starting array
$arr2 = [];
foreach ($arr1 as $arr){
foreach ($arr as $arKey => $arVal) {
if (isset($arr2[$arKey])) {
$arr2[$arKey]['clicks'] += $arVal['clicks'];
$arr2[$arKey]['impressions'] += $arVal['impressions'];
$arr2[$arKey]['ctr'] += $arVal['ctr'];
} else {
$arr2[] = [
'clicks' => $arVal['clicks'],
'impressions' => $arVal['impressions'],
'ctr' => $arVal['ctr'],
];
}
}
}
我希望这是你想做的事情
答案 1 :(得分:0)
像这样:
foreach($array as $key => $value){
if(isset($array2[$key])){
$sum = $array2[$key]+$value;
}
}