-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 
)
8
  • i tried array_diff, however, it returns the value of array1 Commented Nov 19, 2015 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) Commented Nov 19, 2015 at 14:42
  • Can you post a couple of sample arrays, as array_diff should work. Unless the values are not exactly the same. Commented Nov 19, 2015 at 14:42
  • Welcome to Stack Overflow! For future questions, be sure to check out these docs - stackoverflow.com/help/how-to-ask - to create clear, well-documented, well-received posts. Commented Nov 19, 2015 at 14:46
  • This seems like a possible duplicate of Remove item from array if it exists in a 'disallowed words' array with different words, but same goal... Commented Nov 19, 2015 at 14:47

1 Answer 1

4

use array_diff(),

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

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

See this for more info.

Sign up to request clarification or add additional context in comments.

5 Comments

i tried array_diff, however it returns the value of array1 ,
add sample example data for both arrays.
The order in which you pass arguments to array_diff() is important
@MarkBaker what ever i have gien is correct as per the OP
@NiranjanNRaju - I was thinking of the OP's comment that they'd tried it and it didn't work, rather than your own (correct) ordering of the arguments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.