欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php合并二维数组相同的key

程序员文章站 2024-02-08 19:33:16
...
有个二维数组:
$arr = array(
    array('num'=>5,'period'=>3),
    array('num'=>10,'period'=>3),
    array('num'=>15,'period'=>9)
);

对period相同的值的数组,对num求和,怎么处理变成:

$arr = array(
    array('num'=>15,'period'=>3),
    array('num'=>15,'period'=>9)
);

回复内容:

有个二维数组:

$arr = array(
    array('num'=>5,'period'=>3),
    array('num'=>10,'period'=>3),
    array('num'=>15,'period'=>9)
);

对period相同的值的数组,对num求和,怎么处理变成:

$arr = array(
    array('num'=>15,'period'=>3),
    array('num'=>15,'period'=>9)
);

把 @mot 的答案补完优雅一下:

$arr = array(
    array('num'=>5,'period'=>3),
    array('num'=>10,'period'=>3),
    array('num'=>15,'period'=>9)
);
$temp = array();
foreach($arr as $item) {
    list($n, $p) = array_values($item);
    $temp[$p] =  array_key_exists($p, $temp) ? $temp[$p]+$n : $n;
}

$arr = array();
foreach($temp as $p => $n)
    $arr[] = array('num'=>$n, 'period'=>$p);

print_r($arr);

$tmp = array();
foreach( $arr as $one)
{
    if( $tmp[$one['period']] )
        $tmp[$one['period']] = $tmp[$one['period']] + $one['num'];
    else
        $tmp[$one['period']] = $one['num'];
}
$arr = array();
foreach ( $tmp as $key => $value )
    $arr[] = array( 'num' => $value , 'period' => $key );

  function merge($items){

$data = array();
foreach ($items as $v) {
$p = $v['period'];
if(array_key_exists($p, $data)){
$data[$p]['num'] += $v['num'];
}else{
$data[$p] = array('num'=>$v['num'],'period'=>['period']);
}
}
return $data = array_values($data);
}

$arr1 = array(
    array('num'=>5,'period'=>3),
    array('num'=>10,'period'=>3),
    array('num'=>15,'period'=>9)
);

$arr2 = array();

foreach($arr1 as $k1 => $v1) {
    if(empty($arr2)) {
        $arr2[] = $v1;
    } else {
        foreach ($arr2 as &$v2) {
            if($v1['period'] == $v2['period']) {
                $v2['num'] += $v1['num'];   
            } else {
                $arr2[] = $v1;
            }
        }
    }
}

5, 'period'=>3),
    array('num'=>10,'period'=>3),
    array('num'=>15,'period'=>9)
);

$result = array();

$arr = array_reduce($arr, function($prev, $val) use (&$result) {        
    if (empty($prev)) {
        return $val;
    }

    if (isset($prev['period']) && $prev['period'] === $val['period']) {
        $prev['num'] += $val['num'];
    } else {
        $result[] = $val;
        $prev = array();
    }

    empty($prev) || $result[] = $prev;

    return $prev;
}, array());

print_r($result);

$arr = array(
    array('num'=>5,'period'=>3),
    array('num'=>10,'period'=>3),
    array('num'=>15,'period'=>9)
);

$keyvalue = [];

array_filter(
    $arr,
    function ($item) use (&$keyvalue) {
        if (array_key_exists($item['period'], $keyvalue)) {
            $keyvalue[$item['period']] = [
                'period' => $item['period'],
                'num' => $item['num'] + $keyvalue[$item['period']]['num']
            ];
            return false;
        } else {
            $keyvalue[$item['period']] = [
                'period' => $item['period'],
                'num' => $item['num']
            ];
            return true;    
        }
    }
);

var_dump(array_values($keyvalue));

感觉这样可能简单点。

相关标签: php