如何跟踪变量值是否在同一个php while循环中多次使用

I found this snippet in a different question and answer, and it detects if a variable value is the same as the variable value in the previous loop.

$prevValue = NULL;
while(condition) {
 if ($curValue == $prevValue) {
  //do stuff
 }
 $prevValue = $curValue;
}

However what I want to do is to check if a variable value has been used before in the loop, but anywhere in the loop, so if the value happened 1 or 2 or 10 loops ago I want it to tell me if the variable value has come through the loop before.

Use an array to store each value and then check if the current value is in the array:

$prevValues = array();
while(/*condition*/) {
    if (in_array($curValue, $prevValues)) {
        //do stuff
    }
    $prevValues[] = $curValue;
}

As Marc B points out in a comment, you can do it this way as well. It might be marginally faster but I haven't tested it:

while(/*condition*/) {
    if (isset($prevValues[$curValue])) {
        //do stuff
    }
    $prevValues[$curValue] = 1;  //set to whatever
}

Make an array of previouse values and test by in_array function

$prevValue = [];
while(condition) {
 if (in_array($curValue, $prevValue)) {
  //do stuff
 }
 $prevValue[] = $curValue;
}

If you just want to print out unique values, use array_unique. Here is an example:

//This should output 2 3 1 4
$arr = array(2, 3, 2, 1, 4, 4);
foreach(array_unique($arr) as $value){
    echo $value;
}