Skip to content

Instantly share code, notes, and snippets.

@joebordes
Last active February 27, 2018 19:40
Show Gist options
  • Select an option

  • Save joebordes/7fa2b81dee8affc5aaab2e4903313c3b to your computer and use it in GitHub Desktop.

Select an option

Save joebordes/7fa2b81dee8affc5aaab2e4903313c3b to your computer and use it in GitHub Desktop.
find duplicate keys in php array
  • use checkdupkey from the command line to give it file names and get back a list of the ones that have duplicate keys
  • duparraykey.php does the magic and returns 0/1 as an exit code so it can be automated on the command line
  • repeatedlabels.php does the same magic as duparraykey.php but it is prepared to show you a list of the duplicated keys in the browser.

The idea is to run checkdupkey on your arrays and use repeatedlabels.php on the positive ones in order to fix them

License: MIT

HTH

php duparraykey.php $1
if [ $? -gt 0 ]
then
echo $1
fi
<?php
if (empty($argv[1]) || !file_exists($argv[1])) {
exit(1);
} else {
$handle = fopen($argv[1], 'r');
if ($handle) {
$lbls = array();
while (($line = fgets($handle)) !== false) {
if (strpos($line, '=>')>0) {
list($label, $void) = explode('=>', $line);
$label = trim($label);
$label = trim($label, "'");
$label = trim($label, '"');
$lbls[] = $label;
}
}
fclose($handle);
$frec = array_count_values($lbls);
$dups = array_filter($frec, function ($f) {
return ($f>1);
});
$exit = (count($dups)>0 ? 1 : 0);
exit($exit);
} else {
exit(1);
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Language Repeated Labels</title>
<style type="text/css">
body { font-size: 80%; font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif; }
</style>
</head>
<body>
<?php
if (empty($_REQUEST['langfile']) || !file_exists($_REQUEST['langfile'])) {
echo "<br>languagelabels script reads a language file and returns a list of repeated labels<br>";
echo "It is mandatory to launch this script from the root of your install and you must give the full path to the file with the parameter <b>langfile</b><br><br>";
echo "languagelabels.php?langfile=modules/HelpDesk/language/en_us.lang.php<br>";
} else {
$handle = fopen($_REQUEST['langfile'], 'r');
if ($handle) {
$lbls = array();
while (($line = fgets($handle)) !== false) {
if (strpos($line, '=>')>0) {
list($label, $void) = explode('=>', $line);
$label = trim($label);
$label = trim($label, "'");
$label = trim($label, '"');
$lbls[] = $label;
}
}
fclose($handle);
$frec = array_count_values($lbls);
$dups = array_filter($frec, function ($f) {
return ($f>1);
});
if (count($dups)>0) {
echo '<h3>Duplicate labels FOUND</h3>';
foreach ($dups as $lbl => $f) {
echo "'$lbl' found $f times<br>";
}
} else {
echo '<h3>NO duplicate labels in the file</h3>';
}
} else {
echo '<h3>ERROR reading file!</h3>';
}
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment