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, $toUpdate] = array_reduce( $putContent, static function (array $carry, array $item) use ($canonicalIds): array { [$toCreate, $toUpdate] = $carry; if (null === $item['id']) { $toCreate[] = $item; } elseif (true === in_array($item['id'], $canonicalIds, true)) { $toUpdate[$item['id']] = $item; } return [ $toCreate, $toUpdate ]; }, [ [], [] ] ); $toDelete = array_diff($canonicalIds, array_keys($toUpdate)); var_dump($toCreate, $toUpdate, $toDelete);

preferences:
29.12 ms | 408 KiB | 5 Q