比较两个具有不同键名的数组 [英] Comparing two arrays with different key names
问题描述
我需要比较两个包含mySQL表中的值的数组,然后确定不在两个数组之一中的值。我遇到的问题是两个数据库表上的字段名称不同,并且我正在努力找出如何提取值并比较这两个值。
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.
首先,是对数据的初始MySQL调用
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(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(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"
}
}
我想从 $ allMods
中提取数据,其中 module
值不存在于
值 $ indMods
数组的modname
I want to extract the data from $allMods
where the module
value is NOT present in the modname
value of the $indMods
array
有人可以帮忙吗?
谢谢
推荐答案
$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);
这篇关于比较两个具有不同键名的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!