3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = array( array( 'id' => 1, 'timestamp' => 1503050400, 'name' => 'Event A', 'value' => null, ), array( 'id' => 2, 'timestamp' => 1503446400, 'name' => 'Event B', 'value' => null, ), array( 'id' => 2, 'timestamp' => 1503446400, 'name' => 'Event B', 'value' => 71, ), array( 'id' => 3, 'timestamp' => 1503720000, 'name' => 'Event C', 'value' => 12, ), array( 'id' => 3, 'timestamp' => 1503720000, 'name' => 'Event C', 'value' => null, ), ); $buckets = array(); foreach ($data as $item) { $bucketName = serialize(array( 'id' => $item['id'], 'timestamp' => $item['timestamp'], 'name' => $item['name'], )); if (!array_key_exists($bucketName, $buckets)) { $buckets[$bucketName] = array(); } $buckets[$bucketName][] = $item; } $filtered = array_reduce( $buckets, function (array $carry, array $items) { if (1 === count($items)) { $carry[] = array_shift($items); return $carry; } $withoutNullValue = array_filter($items, function (array $item) { return array_key_exists('value', $item) && null !== $item['value']; }); if (0 < count($withoutNullValue)) { $carry = array_merge( $carry, $withoutNullValue ); return $carry; } $carry[] = array_shift($items); return $carry; }, array() ); var_dump($filtered);
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
array(3) { [0]=> array(4) { ["id"]=> int(1) ["timestamp"]=> int(1503050400) ["name"]=> string(7) "Event A" ["value"]=> NULL } [1]=> array(4) { ["id"]=> int(2) ["timestamp"]=> int(1503446400) ["name"]=> string(7) "Event B" ["value"]=> int(71) } [2]=> array(4) { ["id"]=> int(3) ["timestamp"]=> int(1503720000) ["name"]=> string(7) "Event C" ["value"]=> int(12) } }
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/Toq75 on line 54
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/Toq75 on line 54
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/Toq75 on line 54
Process exited with code 255.

preferences:
191.59 ms | 401 KiB | 453 Q