note 129215 added to function.array-unique

From: Date: Thu, 01 Feb 2024 02:19:36 +0000
Subject: note 129215 added to function.array-unique
Groups: php.notes 
Request: Send a blank email to php-notes+get-221169@lists.php.net to get a copy of this message
An other solution to remove duplicates entries of a multi-dimensional array based on key… <?php function array_unique_multi(array $array, string $key): array { $unique = []; foreach ($array as $v) { if (!array_key_exists($v[$key], $unique)) { $unique[$v[$key]] = $v; } } return array_values($unique); } // Usage $unique = array_unique_multi($users, 'id'); ?> Or to preserve keys… <?php function array_unique_amulti(array $array, string $key): array { $keys = []; $unique = []; foreach ($array as $k => $v) { if (!isset($keys[$v[$key]])) { $keys[$v[$key]] = true; $unique[$k] = $v; } } return $unique; } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 2a01:e0a:876:b470:2841:913a:2c60:741f) ---- Manual Page -- https://php.net/manual/en/function.array-unique.php Edit -- https://main.php.net/note/edit/129215 Del: integrated -- https://main.php.net/note/delete/129215/integrated Del: useless -- https://main.php.net/note/delete/129215/useless Del: bad code -- https://main.php.net/note/delete/129215/bad+code Del: spam -- https://main.php.net/note/delete/129215/spam Del: non-english -- https://main.php.net/note/delete/129215/non-english Del: in docs -- https://main.php.net/note/delete/129215/in+docs Del: other reasons-- https://main.php.net/note/delete/129215 Reject -- https://main.php.net/note/reject/129215 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221169) next »