-4

array1 = array();

array2 = array();

Both arrays have values that come from the database;

Array2 contains a list of training, while array1 contains list of training already taken.

I want to compare array1 to array2 and retrieve the list of training that is not in the array1

Here is the snippet of the code i'm currently doing:

$query2 = "SELECT trainingName, rank FROM crewtraininglist WHERE crewId = '$crewId'";
$result2 = mysqli_query($conn, $query2);

$array = array();
while($row = mysqli_fetch_assoc($result2)) 
{        
    $rank = $row['rank'];
    $training = $row['trainingName'];
    $array[] = $row['trainingName'];
    echo "<li>$training</li>";
    //echo "<option value='{$row['name']}'>{$row['name']}</option>";
}

$array2 = array();

$query3 = "SELECT `trainingName`, `rank` FROM `traininglist` WHERE rank LIKE '%$rank%'";
$result3 = mysqli_query($conn, $query3);
while($row = mysqli_fetch_assoc($result3)) 
{
    $array2[]=$row['trainingName'];        
}

$array3 = array_diff($array,$array2);
print_r ($array3);

the output of array 1 is:

Array ( 
    [0] => License + COC & Endrs. 
    [1] => Basic Training + COP 
)

Output of Array2 is:

Array ( 
    [0] => License + COC & Endrs. 
    [1] => Basic Training + COP 
    [2] => Personal Safety & Social Responsibility 
    [3] => Proficiency in Survival Craft & Rescue Boat (PSCRB) + COP 
    [4] => Advance Training in Firefighting (AFF) + COP 
    [5] => Medical Emergency First Aid (MEFA) + COP 
    [6] => Medical Care (MECA) + COP [7] => Radar Observing & Ploting Courses (ROC) 
    [8] => Operational Use of Automatic Radar Ploting Aids (ARPA) 
    [9] => Radar Simulator Course (RSC) 
    [10] => Ship Simulator & Bridge Team Work (SSBT) w/BRM 
)
Kickstart
  • 21,403
  • 2
  • 21
  • 33
Laluna
  • 1
  • 2
  • i tried array_diff, however, it returns the value of array1 – Laluna Nov 19 '15 at 14:36
  • 1
    @Laluna Make a simple example with some test data which you can show us here, to demonstrate how exactly you want to compare the two arrays. Just the two arrays with 3-5 elements and what the goal is to archive. (+ Also add your current attempt/code) – Rizier123 Nov 19 '15 at 14:42
  • Can you post a couple of sample arrays, as array_diff should work. Unless the values are not exactly the same. – Kickstart Nov 19 '15 at 14:42
  • Welcome to Stack Overflow! For future questions, be sure to check out these docs - http://stackoverflow.com/help/how-to-ask - to create clear, well-documented, well-received posts. – jeffdill2 Nov 19 '15 at 14:46
  • This seems like a **possible duplicate** of [Remove item from array if it exists in a 'disallowed words' array](http://stackoverflow.com/questions/1728727/remove-item-from-array-if-it-exists-in-a-disallowed-words-array) with different words, but same goal... – FirstOne Nov 19 '15 at 14:47
  • That will just return an empty array, as everything in array is also in array2 – Kickstart Nov 19 '15 at 15:13
  • both the data from array1 and array2 is comes from the database, i don't need to declare the list of training to be excluded – Laluna Nov 19 '15 at 15:13
  • To get what you want you need to reverse the order of the parameters to _$array3 = array_diff($array2,$array);_ . Note that you could get the same list by joining the 2 tables in SQL. - something like this _SELECT a.trainingName FROM traininglist a LEFT OUTER JOIN crewtraininglist b ON b.crewId = '$crewId' AND a.traininglist = b.crewtraininglist WHERE a.rank LIKE '%$rank%' AND b.crewtraininglist IS NULL_ – Kickstart Nov 19 '15 at 16:27

1 Answers1

4

use array_diff(),

$array1 = array()// from database
$array2 = array()// from database

$array3 = array_diff($array2,$array1);

See this for more info.

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41