3v4l.org

run code in 500+ PHP versions simultaneously
<?php $array = [ [ "id" => '6230061c0e88d709ca0d7bbc', 'name' => 'Mobile SamSung', 'slug' => 'mobile-samsung', 'createdAt' => '1648006346' ], [ "id" => '5d1eff529a426778d4b92383', 'name' => 'Mobile Iphone', 'slug' => 'mobile-iphone', 'createdAt' => '1647314181' ], [ "id" => '5d1eff6b9a426778d4b92dc4', 'name' => 'Mobile SamSung', 'slug' => 'mobile-samsung', 'createdAt' => '1647314460' ], [ "id" => '5f894011266aea580b028cb0', 'name' => 'Mobile LG', 'slug' => 'mobile-lg', 'createdAt' => '1647314456' ], [ "id" => '5f894011266aea580b028cb0', 'name' => 'Mobile LG', 'slug' => 'mobile-lg', 'createdAt' => '1647314458' ], [ "id" => '5f894011266aea580b028cb0', 'name' => 'Mobile LG', 'slug' => 'mobile-lg', 'createdAt' => '1647314457' ], ]; $newArray = []; foreach ($array as $key => $value) { $findIndex = array_search($value['slug'], array_column($newArray, 'slug')); if ($findIndex === false) { $newArray[] = $value; } elseif ($findIndex !== false && $newArray[$findIndex]['createdAt'] <= $value['createdAt']) { $newArray[$findIndex] = $value; } } print_r($newArray);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.21, 8.5.0 - 8.5.7
Array ( [0] => Array ( [id] => 6230061c0e88d709ca0d7bbc [name] => Mobile SamSung [slug] => mobile-samsung [createdAt] => 1648006346 ) [1] => Array ( [id] => 5d1eff529a426778d4b92383 [name] => Mobile Iphone [slug] => mobile-iphone [createdAt] => 1647314181 ) [2] => Array ( [id] => 5f894011266aea580b028cb0 [name] => Mobile LG [slug] => mobile-lg [createdAt] => 1647314458 ) )

preferences:
82.62 ms | 1361 KiB | 4 Q