且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在多维数组中添加值?

更新时间:2022-06-25 09:27:42

试试这个:

$array = array( /* .... */ );
$result = array();

foreach ($array as $item) {
    if (!isset($result[$item['AgentName']])) {
        $result[$item['AgentName']] = array(
             'TotalPosts'        => 0, 
             'AgnetSchemeNumber' => $item['AgentSchemeNumber'], 
             'AgentName'         => $item['AgentName'] 
        );
    }

    $result[$item['AgentName']]['TotalPosts'] += $item['TotalPosts'];
}
$result = array_values($result);

var_dump($result);