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);

preferences:
22.52 ms | 407 KiB | 5 Q