1

I'm working on in_array() method. If the value read is already in the array it should be skipped and proceed to the next value. If the current value is not on the array yet, it should be pushed to the array.

here's my code:

while ($Result_Data_2 = mysqli_fetch_array($Result)){ //130 rows from database
      $Res_Array = array();
      $SQL_Result_Time = $Result_Data_2['Interpretation_Time'];

      /* Some statements here */

      if(in_array($SQL_Result_Time, $Res_Array, true)){
          break;
      }
      else{
          array_push($Res_Array, $Number, $SQL_Questionnaire_ID, $SQL_User_ID, $SQL_Psychology_FirstName, $SQL_Psychology_LastName, $SQL_Result_Date, $SQL_Result_Time);
      }
      echo "<pre>";print_r($Res_Array);echo "</pre>";
}

Problem: It seems that it ignores my condition which is if(in_array($SQL_Result_Time, $Res_Array, true)){break; } and still inserts the value into the array. It still duplicates data

Question:

  1. How to prevent the duplication data where if the current value was found inside the array it would just skip the statement and proceed to another value for checking the array and so on?
  2. Is my logic on checking the value on the array is right?
  • Look carefully at the declaration of `$Res_Array`... – Scott Feb 21 '18 at 14:49
  • Please have a look at this. The difference between break and a continue statement. https://stackoverflow.com/a/4364796/2295484 – Krishnadas PC Feb 21 '18 at 14:52
  • my bad... I just declared it outside the while loop because every loop it erases all values inside the array because of its declaration causing my problem. @Scott –  Feb 21 '18 at 15:04
  • Do you really want to put each item individually into the result array (using `array_push()`) or do you want them as one item? – Nigel Ren Feb 21 '18 at 15:06

2 Answers2

2

You are re-initialising your array on every iteration of the while loop. You should declare it outside of the loop:

$Res_Array = array();
while ($Result_Data_2 = mysqli_fetch_array($Result)){ //130 rows from database
      $SQL_Result_Time = $Result_Data_2['Interpretation_Time'];

      /* Some statements here */

      if(in_array($SQL_Result_Time, $Res_Array, true)){
          break;
      }
      else{
          array_push($Res_Array, $Number, $SQL_Questionnaire_ID, $SQL_User_ID, $SQL_Psychology_FirstName, $SQL_Psychology_LastName, $SQL_Result_Date, $SQL_Result_Time);
      }
      echo "<pre>";print_r($Res_Array);echo "</pre>";
}

Also, as mentioned by Marvin Fischer in his answer, your break statement will terminate the while loop on the first duplicated value. You should instead use continue

while ($Result_Data_2 = mysqli_fetch_array($Result)){ //130 rows from database
      ...
      if(in_array($SQL_Result_Time, $Res_Array, true)){
          continue;
      }
      ....
}

This question should clarify any issues you have with break and continue statements

Scott
  • 1,863
  • 2
  • 24
  • 43
0

First of all, inside of a loop you should use continue, otherwise you cancel the whole loop, secondly you empty $Res_Array at the beginning of every loop purging the old data, inserting the new one and echoing it again

Marvin Fischer
  • 2,552
  • 3
  • 23
  • 34