0

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";
?>

1 Answer 1

1

Rather than saying:

$cnAry[] = $lary;

Do this:

if ( array_key_exists( $lary[0], $keys ) ) {
  // Duplicate!
  $lary[0] = '<<<DUPLICATE>>>';
} else {
  // Original (not dupe)
  $keys[ $lary[0] ] = true;
}
$cnAry[] = $lary;

Here's how it works:

$keys is an array of keys you've seen before. If $lary[0] exists in $keys then it's a dupe and we change $lary[0] to <<<DUPLICATE>>> before appending it to $cnAry.

If $lary[0] doesn't exist in $keys, that means it's original (not a dupe) so we add that key to $keys to track that we've just seen it.

Also, you should probably declare the $keys = []; variable before your loop, to avoid PHP warnings.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Works great. So glad you mentioned declaring $keys = [];. Saved me from another headache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.