如何在 PHP 中比较和获取 2 个 multidim 数组之间的差异
Posted
技术标签:
【中文标题】如何在 PHP 中比较和获取 2 个 multidim 数组之间的差异【英文标题】:How to compare and get the difference between 2 multidim arrays in PHP 【发布时间】:2013-02-09 02:28:23 【问题描述】:有人可以帮我解决这个问题吗?
我有 2 个数组 arr1 和 arr2
<?php
$arr1[0]['name'] = "Ben";
$arr1[0]['level'] = "3";
$arr1[0]['age'] = "10";
$arr1[0]['gender'] = "M";
$arr1[1]['name'] = "Chris";
$arr1[1]['level'] = "12";
$arr1[1]['age'] = "4";
$arr1[1]['gender'] = "F";
$arr2[0]['name'] = "Jack";
$arr2[0]['grade_level'] = "3";
$arr2[0]['age'] = "10";
$arr2[0]['gender'] = "F";
$arr2[1]['name'] = "Lily";
$arr2[1]['level'] = "2";
$arr2[1]['age'] = "7";
$arr2[1]['gender'] = "F";
$arr2[2]['name'] = "Chris";
$arr2[2]['level'] = "12";
$arr2[2]['age'] = "4";
$arr2[2]['gender'] = "M";
?>
我需要在具有相同级别和年龄值的条目上比较 arr1 和 arr2 之间的差异(反之亦然),并返回/突出显示不匹配的列。
例如: 将 arr1 中的 Ben->3->10->M 与 进行比较 A.1 arr2 中的 Jack->3->10->F 因为它们具有相同的级别和年龄值 arr1 中的 Chris->12->4->F 将与进行比较 A.2 Chris->12->4->在 arr2 中的 M 因为他们有相同的 level 和 age 值
我应该能够知道哪些列不匹配 对于 A.1,返回应该是姓名和性别 对于 A.2,返回应该是性别
期望的输出: http://s7.postimage.org/8r3tf24rf/img.png
我已经尝试了 array_diff 和几个数组函数和操作,但我无法得出想要的结果 :( 请帮忙。谢谢!
【问题讨论】:
您的示例只有两个级别。你的真实数据是真的吗? 【参考方案1】:例如,您可以使用如下所示的“辅助数组”($hl 荧光笔):
<?php
$arr1[0]['name'] = "Ben";
$arr1[0]['level'] = "3";
$arr1[0]['age'] = "10";
$arr1[0]['gender'] = "M";
$arr1[1]['name'] = "Chris";
$arr1[1]['level'] = "12";
$arr1[1]['age'] = "4";
$arr1[1]['gender'] = "F";
$arr2[0]['name'] = "Jack";
$arr2[0]['level'] = "3";
$arr2[0]['age'] = "10";
$arr2[0]['gender'] = "F";
$arr2[1]['name'] = "Lily";
$arr2[1]['level'] = "2";
$arr2[1]['age'] = "7";
$arr2[1]['gender'] = "F";
$arr2[2]['name'] = "Chris";
$arr2[2]['level'] = "12";
$arr2[2]['age'] = "4";
$arr2[2]['gender'] = "M";
$hl1 = array();
$hl2 = array();
foreach($arr1 as $x => $tester)
foreach($arr2 as $y => $testing)
if(($tester['level'] == $testing['level']) AND ($tester['age'] == $testing['age']))
foreach($tester as $key => $value)
if($testing[$key] != $value)
$hl1[$x]['pos'] = true;
$hl1[$x]['key'][] = $key;
$hl2[$y]['pos'] = true;
$hl2[$y]['key'][] = $key;
$printheader = true;
echo "<table border='1'>";
foreach($arr1 as $pos => $printer)
echo "<tr>";
if($printheader)
$header = array_keys($printer);
foreach($header as $key)
echo "<td>". $key ."</td>";
echo "</tr><tr>";
$printheader = false;
foreach($printer as $key => $value)
if(isset($hl1[$pos]) AND in_array($key, $hl1[$pos]['key']))
echo "<td style='background-color: ff0000;'>". $value ."</td>";
else
echo "<td>". $value ."</td>";
echo "</tr>";
echo "</table>";
$printheader = true;
echo "<table border='1'>";
foreach($arr2 as $pos => $printer)
echo "<tr>";
if($printheader)
$header = array_keys($printer);
foreach($header as $key)
echo "<td>". $key ."</td>";
echo "</tr><tr>";
$printheader = false;
foreach($printer as $key => $value)
if(isset($hl2[$pos]) AND in_array($key, $hl2[$pos]['key']))
echo "<td style='background-color: ff0000;'>". $value ."</td>";
else
echo "<td>". $value ."</td>";
echo "</tr>";
echo "</table>";
?>
看起来像这样:http://anyimg.com/view/8pj5q71/table_out.png
【讨论】:
以上是关于如何在 PHP 中比较和获取 2 个 multidim 数组之间的差异的主要内容,如果未能解决你的问题,请参考以下文章
如何比较 PHP 5.2.8 中的两个 DateTime 对象?
如何使用 php pdo 中的 2 个表获取 1 个表用户详细信息?