<?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);
preferences:
66.35 ms | 409 KiB | 5 Q