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, ], ]; $toCreate = array_filter($putContent, static function (array $item): bool { return null === $item['id']; }); $toUpdate = array_uintersect( $putContent, array_map(static function (string $id): array { return ['id' => $id]; }, $canonicalIds), static function (array $putItem, array $canonicalItem): int { return $putItem['id'] <=> $canonicalItem['id']; } ); $toDelete = array_diff($canonicalIds, array_column($toUpdate, 'id')); var_dump($toCreate, $toUpdate, $toDelete);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.29, 8.2.0 - 8.2.20, 8.3.0 - 8.3.8
array(1) { [3]=> 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" }

preferences:
82.36 ms | 403 KiB | 94 Q