I have a script that allows a user to submit a form. Each form submission creates a new line (array) which is displayed in a textarea named $pvrbs['pramtrs']. Each value of each array is separated by a double-pipe in the textarea. When the user finishes adding lines to the textarea, he clicks on a button which saves the textarea to a flat file.
Below is a snippet of the function that creates the multidimensional array and the flat file:
function create_pcFile($pName) {
$pcnf = "<" . "?php \n";
$pur = explode("\n",stripslashes(trim($pvrbs['pramtrs'])));
$pcnt = 0;
$uses = 0;
$cnAry = array();
foreach ($pur as $pline) {
$pline = trim($pline);
$lary = explode("||",$pline);
if ( !verify_cd($lary[0]) ) { $lary[0] = "<<<ERROR>>>"; }
if ( !verify_url($lary[1]) ) { $lary[1] = "<<<ERROR>>>"; }
if ( !verify_securerpage($lary[2]) ) { $lary[2] = "<<<ERROR>>>"; }
if ( !verify_url($lary[3]) ) { $lary[3] = "<<<ERROR>>>"; }
if ( !verify_date($lary[4]) ) { $lary[4] = "<<<ERROR>>>"; }
if ( !verify_date($lary[5]) ) { $lary[5] = "<<<ERROR>>>"; }
if ( !verify_uss($lary[6]) ) { $lary[6] = "<<<ERROR>>>"; }
if( (isset($lary[0])) && (isset($lary[1])) && (isset($lary[2])) && (isset($lary[3])) && (isset($lary[4])) && (isset($lary[5])) && (isset($lary[6]))) {
$cnAry[] = $lary;
}
}
$Stg = '';
if(is_array($cnAry) && !empty($cnAry) ) {
foreach ($cnAry as $ary) {
$Stg .= "array(\"" . $ary[0] . "\", \"" . $ary[1] . "\", \"" . $ary[2] . "\", \"" . $ary[3] . "\", \"" . $ary[4] . "\", \"" . $ary[5] . "\", \"" . $ary[6] . "\"), ";
}
}
$pcnf .= "$" . "params = array(" . rtrim($parStg, ", ") . ");\n"; // Trim ", " off of final array.
$pcnf .= "$" . "erPage = \"" . $pvrbs['erPage'] . "\";\n";
$pcnf .= "?" . ">";
$x = start_file($pName, $pcnf);
return;
}
Below is an example of the textarea with a duplicate 111:
111||https://1.com/||No||https://e.com/||07/12/2020||07/30/2021||2
222||https://2.com||No||https://3.com||07/30/2020||07/30/2021||5
111||https://6.com||No||https://4.net||07/30/2020||07/30/2021||7
Below is the flat file that corresponds to the textarea above:
<?php
$params = array(array("111", "https://1.com/", "No", "https://e.com/", "07/12/2020", "07/30/2021", "2"), array("222", "https://2.com", "No", "https://3.com", "07/30/2020", "07/30/2021", "5"), array("111", "https://6.com", "No", "https://4.net", "07/30/2020", "07/30/2021", "7"));
$erPage = "https://t.com";
?>
What code do I need to add to the function that will find and replace duplicates? I need the first value of each array $lary[0] to be unique. Using the examples above, the second instance of 111 should be replaced with <<<DUPLICATE>>>.
Below is what the textarea should look like once the duplicate has been replaced:
111||https://1.com/||No||https://e.com/||07/12/2020||07/30/2021||2
222||https://2.com||No||https://3.com||07/30/2020||07/30/2021||5
<<<DUPLICATE>>>||https://6.com||No||https://4.net||07/30/2020||07/30/2021||7
Below is what the flat file should look like:
<?php
$params = array(array("111", "https://1.com/", "No", "https://e.com/", "07/12/2020", "07/30/2021", "2"), array("222", "https://2.com", "No", "https://3.com", "07/30/2020", "07/30/2021", "5"), array("<<<DUPLICATE>>>", "https://6.com", "No", "https://4.net", "07/30/2020", "07/30/2021", "7"));
$erPage = "https://t.com";
?>