Chris
Chris

Reputation: 1883

Comparing two arrays with different key names

I need to compare two arrays containing values from mySQL tables, then identify the values which are NOT in one of the two arrays. The problem I'm having is that the field names on the two DB tables are different, and I'm struggling to work out how to extract the values and compare the two.

I'll list all of the code that relates to this - hopefully someone will be able to help.

First, the initial call to MySQL for the data

$sql1 = "SELECT modname FROM users_modules WHERE email='$email'";
$result1 = $conn->query($sql1);
while ($row = $result1->fetch_assoc()){
   $indMods[] = $row;
}

$sql2 = "SELECT module, mod_name, level FROM modules WHERE level = '$level'";
$result2 = $conn->query($sql2);
while ($row2 = $result2 -> fetch_assoc()){
   $allMods[] = $row2;
}

This returns the following two arrays:

$indMods array

array(5) {
  [0]=>
  array(1) {
    ["modname"]=>
    string(3) "302"
  }
  [1]=>
  array(1) {
    ["modname"]=>
    string(3) "303"
  }
  [2]=>
  array(1) {
    ["modname"]=>
    string(3) "304"
  }
  [3]=>
  array(1) {
    ["modname"]=>
    string(3) "305"
  }
  [4]=>
  array(1) {
    ["modname"]=>
    string(3) "306"
  }
}

$allMods array

array(10) {
  [0]=>
  array(3) {
    ["module"]=>
    string(3) "301"
    ["mod_name"]=>
    string(42) "3.01 Introduction to Facilities Management"
    ["level"]=>
    string(1) "3"
  }
  [1]=>
  array(3) {
    ["module"]=>
    string(3) "302"
    ["mod_name"]=>
    string(35) "3.02 CSR & Sustainability in FM"
    ["level"]=>
    string(1) "3"
  }
  [2]=>
  array(3) {
    ["module"]=>
    string(3) "303"
    ["mod_name"]=>
    string(47) "3.03 Customer & Stakeholder Relations in FM"
    ["level"]=>
    string(1) "3"
  }
  [3]=>
  array(3) {
    ["module"]=>
    string(3) "304"
    ["mod_name"]=>
    string(39) "3.04 FM Specification & Procurement"
    ["level"]=>
    string(1) "3"
  }
  [4]=>
  array(3) {
    ["module"]=>
    string(3) "305"
    ["mod_name"]=>
    string(41) "3.05 Health & Safety Responsibilities"
    ["level"]=>
    string(1) "3"
  }
  [5]=>
  array(3) {
    ["module"]=>
    string(3) "306"
    ["mod_name"]=>
    string(44) "3.06 Project Management within FM Operations"
    ["level"]=>
    string(1) "3"
  }
  [6]=>
  array(3) {
    ["module"]=>
    string(3) "307"
    ["mod_name"]=>
    string(25) "3.07 FM Budget Management"
    ["level"]=>
    string(1) "3"
  }
  [7]=>
  array(3) {
    ["module"]=>
    string(3) "308"
    ["mod_name"]=>
    string(45) "3.08 FM within the context of an organisation"
    ["level"]=>
    string(1) "3"
  }
  [8]=>
  array(3) {
    ["module"]=>
    string(3) "311"
    ["mod_name"]=>
    string(31) "3.11 Building Maintenance in FM"
    ["level"]=>
    string(1) "3"
  }
  [9]=>
  array(3) {
    ["module"]=>
    string(3) "313"
    ["mod_name"]=>
    string(46) "3.13 Disaster Recovery & Contingency Plans"
    ["level"]=>
    string(1) "3"
  }
}

I want to extract the data from $allMods where the module value is NOT present in the modname value of the $indMods array

Can anyone help?

Thanks

Upvotes: 0

Views: 94

Answers (2)

m59
m59

Reputation: 43755

$x = [];
foreach ($indMods as $v) {
  array_push($x, $v['modname']);
}

$extracted = [];
foreach ($allMods as $k => $v) {
  $modValue = $v['module'];
  if (!in_array($modValue, $x)) {
    array_push($extracted, $v);
  }
}

var_dump($extracted);

Upvotes: 0

Jasper
Jasper

Reputation: 76003

Give one of or both of the fields an alias so they match-up key wise:

$sql1 = "SELECT modname AS module FROM users_modules WHERE email='$email'";

Now there is a key of module rather than modname.

You can use array_diff to find the difference between arrays as long as they have similar structure. In your case this would be removing the other two keys from your second DB query.

For example:

$sql1 = "SELECT modname AS module FROM users_modules WHERE email='$email'";
$result1 = $conn->query($sql1);
while ($row = $result1->fetch_assoc()){
   $indMods[] = $row;
}

$sql2 = "SELECT module FROM modules WHERE level = '$level'";
$result2 = $conn->query($sql2);
while ($row2 = $result2 -> fetch_assoc()){
   $allMods[] = $row2;
}

$difference = array_diff($indMods, $allMods);

Documentation for PHP's array_diff: http://php.net/manual/en/function.array-diff.php

Note that there are many ways to do this. This is just one.

Upvotes: 3

Related Questions