3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ ['id' => 1, 'value' => 's'], ['id' => 2, 'value' => 'b'], ['id' => 3, 'value' => 'c'], ['id' => 4, 'value' => '5'], ['id' => 5, 'value' => 'e'], ['id' => 6, 'value' => 'j'], ]; $ids = array_column($array, 'id'); $filtered = array_filter($array, function ($el) { return $el['value'] === 'a'; }); $toDelete = $ids; $toUpdate = null; if (!empty($filtered)) { $lastAIndex = max(array_keys($filtered)); $toDelete = array_diff($toDelete, [$array[$lastAIndex]['id']]); $toUpdate = null; } else { $toUpdate = end($array)['id']; $toDelete = array_diff($toDelete, [$toUpdate]); } $result = [ 'toDelete' => array_values($toDelete), 'toUpdate' => $toUpdate, ]; print_r($result);
Output for 8.2.0 - 8.2.27, 8.3.0 - 8.3.16, 8.4.1 - 8.4.3
Array ( [toDelete] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) [toUpdate] => 6 )

preferences:
40.97 ms | 406 KiB | 5 Q