开发者

I got many duplicates with this function

开发者 https://www.devze.com 2023-04-13 05:57 出处:网络
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 passe

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?

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

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

}
return $result;
}

The output of array1 and array2 is:

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







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


You could simplify the inner if() DRASTICALLY by doing this:

if($array1[$i]['lesson'] == $lesson2['lesson_id'] && $array1[$i]['user_id'] == $lesson2['user_id']){
    $completion = 'Complete';
} else {
    $completion = 'Incomplete';
}
$result[] = array(
   'first_name' => $array1[$i]['first_name'],
   'last_name' => $array1[$i]['last_name'],
   'date_of_birth' => $array1[$i]['date_of_birth'],
   'user_id' => $array1[$i]['user_id'],
   'client_id' => $array1[$i]['client_id'],
   'lesson' => $array1[$i]['lesson'],
   'lessonName' => $array1[$i]['lessonName'],
   'completion' => $completion // <---- the **ONLY** difference between the two versions
);

As far as your "duplication" problem... I'm not sure what you're trying to accomplish with this. You're running a nested loop, with 5 outer and 12 inner members, which gives your 60 array entries. Since you're assigning something to $result inside the inner loop, no matter WHAT the if() condition returns, that'll produce 60 assignments.

If you don't want the 'false' ones to happen, then don't do anything in the else clause of your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消