Voting

: seven plus one?
(Example: nine)

The Note You're Voting On

tasiot
1 year ago
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;
}

?>

<< Back to user notes page

To Top