3v4l.org

run code in 300+ PHP versions simultaneously
<?php $canonicalIds = [ 'first', 'second', 'third', 'fourth', 'fifth', 'sixth', ]; $putContent = [ [ 'id' => 'fifth', 'value' => 55, ], [ 'id' => 'second', 'value' => 22, ], [ 'id' => 'fourth', 'value' => 44, ], [ 'id' => null, 'value' => 7, ], ]; function mySort(array $canonicalIds, array $putContent): array { $toCreate = $toUpdate = []; foreach ($putContent as $putItem) { if (null === $putItem['id']) { $toCreate[] = $putItem; } elseif (in_array($putItem['id'], $canonicalIds, true) === true) { $toUpdate[] = $putItem; } } $toDelete = array_diff($canonicalIds, array_column($toUpdate, 'id')); return [$toCreate, $toUpdate, $toDelete]; } [$toCreate, $toUpdate, $toDelete] = mySort($canonicalIds, $putContent); var_dump($toCreate, $toUpdate, $toDelete);
Output for git.master, git.master_jit, rfc.property-hooks
array(1) { [0]=> array(2) { ["id"]=> NULL ["value"]=> int(7) } } array(3) { [0]=> array(2) { ["id"]=> string(5) "fifth" ["value"]=> int(55) } [1]=> array(2) { ["id"]=> string(6) "second" ["value"]=> int(22) } [2]=> array(2) { ["id"]=> string(6) "fourth" ["value"]=> int(44) } } array(3) { [0]=> string(5) "first" [2]=> string(5) "third" [5]=> string(5) "sixth" }

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