I got many duplicates with this function
<p>I know something wrong with this function. It is supposed to return an array with 12 keys and values but it returns 60. The problem should be the foreach loop. Everytime the if loop is false, it passes a value to $result. Can anyone tell me how to make it right?</p>

<pre><code>function getFinaluserElearningLesson($array1, $array2){
 $result = array();
for($i = 0; $i&lt;count($array1); $i++){
foreach($array2 as $lesson2){

  if($array1[$i]['lesson'] == $lesson2['lesson_id'] &amp;&amp; $array1[$i]['user_id'] == $lesson2['user_id']){
    $result[] = array(
       'first_name' =&gt; $array1[$i]['first_name'],
       'last_name' =&gt; $array1[$i]['last_name'],
       'date_of_birth' =&gt; $array1[$i]['date_of_birth'],
       'user_id' =&gt; $array1[$i]['user_id'],
       'client_id' =&gt; $array1[$i]['client_id'],
       'lesson' =&gt; $array1[$i]['lesson'],
       'lessonName' =&gt; $array1[$i]['lessonName'],
       'completion' =&gt; 'Completed'
    );
  }else{
    $result[] = array(
       'first_name' =&gt; $array1[$i]['first_name'],
       'last_name' =&gt; $array1[$i]['last_name'],
       'date_of_birth' =&gt; $array1[$i]['date_of_birth'],
       'user_id' =&gt; $array1[$i]['user_id'],
       'client_id' =&gt; $array1[$i]['client_id'],
       'lesson' =&gt; $array1[$i]['lesson'],
       'lessonName' =&gt; $array1[$i]['lessonName'],
       'completion' =&gt; 'Incomplete'
    );
  }
}

}
return $result;
}
</code></pre>

<p>The output of array1 and array2 is: </p>

<pre><code>array(12) {
[0]=&gt;
  array(7) {
   ["first_name"]=&gt;
   string(3) "Bob"
   ["last_name"]=&gt;
   string(6) "Gorsky"
   ["date_of_birth"]=&gt;
   string(10) "1954-04-27"
   ["user_id"]=&gt;
   string(7) "1701011"
   ["client_id"]=&gt;
   string(2) "71"
   ["lesson"]=&gt;
   string(3) "222"
   ["lessonName"]=&gt;
   string(6) "Asthma"
 }
[1]=&gt;
  array(7) {
   ["first_name"]=&gt;
   string(3) "Bob"
   ["last_name"]=&gt;
   string(6) "Gorsky"
   ["date_of_birth"]=&gt;
   string(10) "1954-04-27"
   ["user_id"]=&gt;
   string(7) "1701011"
   ["client_id"]=&gt;
   string(2) "71"
   ["lesson"]=&gt;
   string(2) "69"
   ["lessonName"]=&gt;
   string(23) "Cardiac Catheterization"
}
 [2]=&gt;
  array(7) {
   ["first_name"]=&gt;
   string(3) "Bob"
   ["last_name"]=&gt;
   string(6) "Gorsky"
   ["date_of_birth"]=&gt;
   string(10) "1954-04-27"
   ["user_id"]=&gt;
   string(7) "1701011"
   ["client_id"]=&gt;
   string(2) "71"
   ["lesson"]=&gt;
   string(3) "308"
   ["lessonName"]=&gt;
   string(38) "CyberKnife Radiosurgery For Brain AVMs"
}
[3]=&gt;
  array(7) {
   ["first_name"]=&gt;
   string(3) "Bob"
   ["last_name"]=&gt;
   string(6) "Gorsky"
   ["date_of_birth"]=&gt;
   string(10) "1954-04-27"
   ["user_id"]=&gt;
   string(7) "1701011"
   ["client_id"]=&gt;
   string(2) "71"
   ["lesson"]=&gt;
   string(2) "50"
   ["lessonName"]=&gt;
   string(47) "Durable Power of Attorney for Health Care (DPA)"
}
[4]=&gt;
  array(7) {
   ["first_name"]=&gt;
   string(3) "Bob"
   ["last_name"]=&gt;
   string(6) "Gorsky"
   ["date_of_birth"]=&gt;
   string(10) "1954-04-27"
   ["user_id"]=&gt;
   string(7) "1701011"
   ["client_id"]=&gt;
   string(2) "71"
   ["lesson"]=&gt;
   string(2) "91"
   ["lessonName"]=&gt;
   string(12) "Heart Attack"
}
[5]=&gt;
  array(7) {
   ["first_name"]=&gt;
   string(4) "Alex"
   ["last_name"]=&gt;
   string(10) "Wilczewski"
   ["date_of_birth"]=&gt;
   string(10) "1989-05-27"
   ["user_id"]=&gt;
   string(7) "2602204"
   ["client_id"]=&gt;
   string(2) "71"
   ["lesson"]=&gt;
   string(3) "500"
   ["lessonName"]=&gt;
   string(33) "Abdominal Aortic Aneurysm Surgery"
}
[6]=&gt;
 array(7) {
   ["first_name"]=&gt;
   string(4) "Alex"
   ["last_name"]=&gt;
   string(10) "Wilczewski"
   ["date_of_birth"]=&gt;
   string(10) "1989-05-27"
   ["user_id"]=&gt;
   string(7) "2602204"
   ["client_id"]=&gt;
   string(2) "71"
   ["lesson"]=&gt;
   string(1) "1"
   ["lessonName"]=&gt;
   string(19) "Cholesterol Control"
}
[7]=&gt;
 array(7) {
   ["first_name"]=&gt;
   string(4) "Alex"
   ["last_name"]=&gt;
   string(10) "Wilczewski"
   ["date_of_birth"]=&gt;
   string(10) "1989-05-27"
   ["user_id"]=&gt;
   string(7) "2602204"
   ["client_id"]=&gt;
   string(2) "71"
   ["lesson"]=&gt;
   string(1) "5"
   ["lessonName"]=&gt;
   string(16) "Types of Doctors"
}
[8]=&gt;
 array(7) {
   ["first_name"]=&gt;
   string(4) "Alex"
   ["last_name"]=&gt;
   string(2) "Hu"
   ["date_of_birth"]=&gt;
   string(10) "1983-09-14"
   ["user_id"]=&gt;
   string(7) "2685058"
   ["client_id"]=&gt;
   string(2) "71"
   ["lesson"]=&gt;
   string(3) "500"
   ["lessonName"]=&gt;
   string(33) "Abdominal Aortic Aneurysm Surgery"
}
[9]=&gt;
 array(7) {
   ["first_name"]=&gt;
   string(4) "Alex"
   ["last_name"]=&gt;
   string(2) "Hu"
   ["date_of_birth"]=&gt;
   string(10) "1983-09-14"
   ["user_id"]=&gt;
   string(7) "2685058"
   ["client_id"]=&gt;
   string(2) "71"
   ["lesson"]=&gt;
   string(3) "116"
   ["lessonName"]=&gt;
   string(4) "Acne"
}
[10]=&gt;
 array(7) {
   ["first_name"]=&gt;
   string(4) "Alex"
   ["last_name"]=&gt;
   string(2) "Hu"
   ["date_of_birth"]=&gt;
   string(10) "1983-09-14"
   ["user_id"]=&gt;
   string(7) "2685058"
   ["client_id"]=&gt;
   string(2) "71"
   ["lesson"]=&gt;
   string(3) "122"
   ["lessonName"]=&gt;
   string(5) "Burns"
}
[11]=&gt;
 array(7) {
   ["first_name"]=&gt;
   string(4) "Alex"
   ["last_name"]=&gt;
   string(2) "Hu"
   ["date_of_birth"]=&gt;
   string(10) "1983-09-14"
   ["user_id"]=&gt;
   string(7) "2685058"
   ["client_id"]=&gt;
   string(2) "71"
   ["lesson"]=&gt;
   string(3) "546"
   ["lessonName"]=&gt;
   string(60) "Peripherally Inserted Central Catheter (PICC) for Outpatient"
  }
}







 array(5) {
 [0]=&gt;
  array(2) {
   ["user_id"]=&gt;
   string(6) "951301"
   ["lesson_id"]=&gt;
   string(2) "35"
 }
[1]=&gt;
 array(2) {
   ["user_id"]=&gt;
   string(7) "1700981"
   ["lesson_id"]=&gt;
   string(2) "35"
 }
[2]=&gt;
 array(2) {
   ["user_id"]=&gt;
   string(7) "2585455"
   ["lesson_id"]=&gt;
   string(2) "25"
 }
[3]=&gt;
 array(2) {
   ["user_id"]=&gt;
   string(7) "2602204"
   ["lesson_id"]=&gt;
   string(3) "500"
 }
[4]=&gt;
  array(2) {
   ["user_id"]=&gt;
   string(7) "2642677"
   ["lesson_id"]=&gt;
   string(3) "180"
 }
}
</code></pre>
