3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ [ "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' ] ]; $result = []; foreach ($arr as $row) { if (!isset($result[$row['name']]) || (int)$row['createdAt'] > (int)$result[$row['name']]['createdAt']) { $result[$row['name']] = $row; } } var_export(array_values($result));
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
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' => '1647314456', ), )

preferences:
140.02 ms | 403 KiB | 121 Q