我有很多重复这个功能

时间:2011-10-14 15:36:49

标签: php

我知道这个功能有问题。它应该返回一个包含12个键和值的数组,但它返回60.问题应该是foreach循环。每次if循环为false时,它都会将值传递给$ result。有人能告诉我如何做对吗?

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;
}

array1和array2的输出是:

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"
 }
}

1 个答案:

答案 0 :(得分:1)

你可以通过这样做来简化内部if()DRASTICALLY:

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
);

至于你的“重复”问题......我不确定你要用这个来完成什么。你正在运行一个嵌套循环,有5个外部成员和12个内部成员,它们提供了60个数组条目。由于你在内部循环中为$result分配了一些东西,无论if()条件返回什么,这都会产生60个赋值。

如果您不希望发生'false',那么请不要在代码的else子句中执行任何操作。