3v4l.org

run code in 300+ PHP versions simultaneously
<?php function merge(array ...$sets) { /** * group contributions by name */ $contributions = array_reduce( $sets, function (array $contributions, array $set) { foreach ($set as $element) { $name = $element['P_Name']; $contribution = $element['Contribution']; if (!array_key_exists($name, $contributions)) { $contributions[$name] = []; } $contributions[$name][] = $contribution; } return $contributions; }, [] ); /** * normalize the array so we remove the name as key, and return a tuple of name and contribution, with the desired * structure */ return array_values(array_map(function (array $contribution, $name) { return [ 'Contribution' => array_sum($contribution), 'P_Name' => $name, ]; }, $contributions, array_keys($contributions))); } $a = [ [ 'Contribution' => 1000, 'P_Name' => 'A', ], [ 'Contribution' => 1500, 'P_Name' => 'B', ], ]; $b = [ [ 'Contribution' => 100, 'P_Name' => 'A', ], [ 'Contribution' => 200, 'P_Name' => 'B', ], ]; $merged = merge($a, $b); var_dump($merged);
Output for git.master, git.master_jit, rfc.property-hooks
array(2) { [0]=> array(2) { ["Contribution"]=> int(1100) ["P_Name"]=> string(1) "A" } [1]=> array(2) { ["Contribution"]=> int(1700) ["P_Name"]=> string(1) "B" } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
65.09 ms | 401 KiB | 8 Q