有人可以帮我解决这个问题。
我有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中的3-> 10-> F因为它们具有相同的水平和年龄值
将arr1中的Chris-> 12-> 4-&gt; F与
进行比较
A.2 Chris-&gt;&gt;&gt; 4-&gt; M在arr2中,因为它们具有相同的水平和年龄值
我应该能够知道哪些列不匹配
对于A.1,返回应该是名称&amp;性别
对于A.2,回报应该是性别
期望的输出: http://s7.postimage.org/8r3tf24rf/img.png
我尝试过array_diff和几个数组函数&amp;操纵,但我无法想出预期的结果:( 请帮助。谢谢!
答案 0 :(得分:0)
例如,您可以使用“helper arrays”($ hl highlighter),如下所示:
<?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>";
?>