0

有人可以帮我解决这个问题。

我有 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 将与
arr2 中的 A.1 Jack->3->10->F 进行比较,因为它们具有相同的级别和年龄值

Chris->12->4- arr1 中的 >F 将与
arr2 中的 A.2 Chris->12->4->M 进行比较,因为它们具有相同的级别和年龄值

我应该能够知道哪些列与
A.1 不匹配返回应该是 A.2 的名称和性别
返回应该是性别

所需输出: http ://s7.postimage.org/8r3tf24rf/img.png

我已经尝试了 array_diff 和几个数组函数和操作,但我无法得出想要的结果:(请帮助。谢谢!

4

1 回答 1

0

例如,您可以使用如下所示的“辅助数组”($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

于 2013-02-09T04:20:18.223 回答