betterCode() PHP 2025

Voting

: zero plus four?
(Example: nine)

The Note You're Voting On

Ray dot Paseur at SometimesUsesGmail dot com
17 years ago
I needed to identify email addresses in a data table that were replicated, so I wrote the array_not_unique() function:

<?php

function array_not_unique($raw_array) {
$dupes = array();
natcasesort($raw_array);
reset ($raw_array);

$old_key = NULL;
$old_value = NULL;
foreach (
$raw_array as $key => $value) {
if (
$value === NULL) { continue; }
if (
$old_value == $value) {
$dupes[$old_key] = $old_value;
$dupes[$key] = $value;
}
$old_value = $value;
$old_key = $key;
}
return
$dupes;
}

$raw_array = array();
$raw_array[1] = 'abc@xyz.com';
$raw_array[2] = 'def@xyz.com';
$raw_array[3] = 'ghi@xyz.com';
$raw_array[4] = 'abc@xyz.com'; // Duplicate

$common_stuff = array_not_unique($raw_array);
var_dump($common_stuff);
?>

<< Back to user notes page

AltStyle によって変換されたページ (->オリジナル) /