0

I am having a weird problem in PHP. I have an array with some integer values in it. When I try to compare the values in the array to a target integer, the index does not work with the array and also cannot be incremented. Here is an example.

The problem lies in the if($j == $indexOfExposed[$k])

/*  This is a simple function to show some letters of a patient name, while the rest of letters are replaced with asterisks.
    @Param: Patient name
    @Return: limited preview of patient name;
*/
function encodeName($name){

    $len = strlen($name);                       // This is the lenghth of $name. used to itterate and set a range.
    $numOfExposedLetters = rand(1, 4);          // Random value assigned to see how many letters of the name will be exposed.
    $indexOfExposed = array();                  // This is an array of indexes for exposed letters.
    $k = 0;                                     // This counter is used to traverse the $indexOfExposed array.
    $encodedName = "";                          // This is the value we will return after it is generated.
    $previous = 0;                              // Used to keep track of previous value in the $indexOfExposed array. This is incase we have repeated values in the array;

    /*  This loop is used to append random values to the arary of indexes,
        With $numOfExposedLetters being the quantity of exposed letters.    */
    for($i = 0; $i < $numOfExposedLetters; $i++){
        $indexOfExposed[$i] = rand(2, $len);
    }
    sort($indexOfExposed);                      // Sort the array, for easier access.


    /* Ecoding name */
    for($j = 1; $j <= $len; $j++){

        if($indexOfExposed[$k] == $previous){
            $encodedName .= "*";
            $k++;
            continue;
        }

        if($j == $indexOfExposed[$k]){
            $encodedName .= $name[$j-1];
            $previous = $indexOfExposed[$k];
            $k++;
        }

        else
            $encodedName .= "*";
            $k++;

    }   // end of encode

    return $encodedName;

}

This code takes in a person's name and replaces the letters in the name with asterisks. random indexes with expose the actual letters in the name.

6
  • 2
    if($indexOfExposed[$k] = $previous){ thats not a comparison, its an equal, use ==, learn to debug... Commented Mar 3, 2017 at 20:43
  • Hey man, chill out. It's not like you have never missed an extra equal sign haha. Thanks for the catch though! probably happened when I was DEBUGGING and accidently deleted the extra = Commented Mar 3, 2017 at 20:48
  • 1
    @RicardoOrtegaMagaña This error happens dozens of times a day here, get used to it. Point out the error politely, flag the question for closing as a typo, and move on. Commented Mar 3, 2017 at 20:53
  • Not a typo. That does not solve the problem. I just accidently deleted when i posted the code onto StackOverflow. Commented Mar 3, 2017 at 20:55
  • 3
    Your algorithm is too complicated. The entire thing can be sorted out (after you generate $indexOfExposed) in three lines of code: $encodedName = $name; foreach ($indexOfExposed as $idx) { $encodedName[$idx] = '*'; }. There is no need to sort $indexOfExposed (there is even no need to generate it, you can do the encoding on the fly). It doesn't expose but it masks the characters whose indices are generated in $indexOfExposed but as long as the indices are randomly generated, it doesn't make any difference. Commented Mar 3, 2017 at 21:01

1 Answer 1

2

Debugging your code is a tedious work that is not worth it. I suggest you an alternative implementation that, hopefully, uses the algorithm you have in mind, is easier to read and understand and probably runs faster.

function encodeName($name)
{
    $len = strlen($name);
    $numOfExposedLetters = rand(1, 4);
    $encodedName = str_repeat('*', $len);
    while ($numOfExposedLetters --) {
        $index = rand(2, $len - 1);
        $encodedName[$index] = $name[$index];
    }

    return $encodedName;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Beautiful code. Thank you for this implementation. I will still try to debug my previous code so I can keep my sanity. But I will definitely use a version of your implementation.
This is not guaranteed to expose $numOfExposedLetters letters. When the random index is the same all the time, only one letter is exposed.
Yeah, but this is okay though. I only need to expose the letter once!

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.