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 git.master, git.master_jit, rfc.property-hooks
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) } }

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:
125.23 ms | 406 KiB | 5 Q