慕森卡
使用功能array_reduce()组合具有相同功能的项目city:$input = array( array('city' => 'NewYork', 'cash' => '1000'), array('city' => 'Philadelphia', 'cash' => '2300'), array('city' => 'NewYork', 'cash' => '2000'),);$output = array_reduce( // Process the input list $input, // Add each $item from $input to $carry (partial results) function (array $carry, array $item) { $city = $item['city']; // Check if this city already exists in the partial results list if (array_key_exists($city, $carry)) { // Update the existing item $carry[$city]['cash'] += $item['cash']; } else { // Create a new item, index by city $carry[$city] = $item; } // Always return the updated partial result return $carry; }, // Start with an empty list array());