比较两个不同的多维数组并突出显示更改 [英] Compare two different multidimentional arrays and highlight the changes

查看:66
本文介绍了比较两个不同的多维数组并突出显示更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个日志文件,该文件中我想同时显示旧条目和更新条目,但是与此同时,我也想突出显示新条目中的更改,就像我们编辑问题时在stackoverflow中所做的一样它们突出显示了已编辑的部分.

I am creating a log file in which I want to show the old entry and the updated entry both together, but along those I also want to highlight the changes in the new entry like it's being done in stackoverflow when we edit the question they highlight the edited part.

如您所见,描述有所不同,并且还有许多其他索引,我只展示其中一些.

As you can see there is a difference in the description and there are many other indexes, I am just showing some of them.

我有两个不同的数组,如下所示:

I have two different arrays that look like this:

新条目

   Array
(
    [0] => stdClass Object
        (

            [business_logo] => 31190_photo.jpg
            [business] => Sms Factory Powered By Bds Technologies Pvt Ltd
            [b_discription] => We smsfactory are world's leading SMS messaging provider offering remarakable and reliable SMS Text and Voice messaging globally through almost all-networks of mobile phones successfully. You may contact-us anytime for making any query. Our Services are very useful economically as well as eco-friendly,
            [mod_date] => 1467736200
        )

)

旧条目

  Array
(
    [0] => stdClass Object
        (
            [business_logo] => 31190_photo.jpg
            [business] => Sms Factory Powered By Bds Technologies Pvt Ltd
            [b_discription] => We smsfactory are world's leading SMS messaging provider offering remarakable and reliable SMS Text and Voice messaging globally through almost all-networks of mobile phones successfully. You may contact-us anytime for making any query. Our Services are very useful economically as well as eco-friendly, which enables you to send simultaneous bulk sms to your targeted Customers, Regular-Customers, Buyers, Shoppers, Fans, Regular-shoppers, Clients, Clientele, Members, Managers, Supervisors, Fieldworkers, Graduates, Post graduates, Technicians, Public, Citizens, Mobile-Users, Viewers, Future-purchasers, Users, End-users, Students, Job-seekers, Enjoyers, Visitors, Frequent-visitors, Persons, Individuals, Frequenter, Obtainers, Receivers, Assignees, Recipients, Travelers, Tourists, Guys, Persons, Men and Women, Spectators, Technicians, Staff, Workers, Recruiters, Newcomers, Representatives, Dealers, Distributors, Followers, Shareholders, Investors, Bondholders, Shareowners, Financiers, Bankers, Participants, Associates, Assistants, Colleagues, Contributors, Helpers, Partakers, Party, Sharers, Supporters, Admirers, Devotees, Groupies, Enthusiasts and many more.
            [mod_date] => 1467736200
        )

)

我为此进行了研究,发现了很多功能,从中可以尝试以下功能:

I did my research for this and found plenty of functions, from which I have tried this:

 function arrayRecursiveDiff($aArray1, $aArray2) { 
    $aReturn = array(); 

    foreach ($aArray1 as $mKey => $mValue) { 
        if (array_key_exists($mKey, $aArray2)) { 
            if (is_array($mValue)) { 
                $aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]); 
                if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; } 
            } else { 
                if ($mValue != $aArray2[$mKey]) { 
                    $aReturn[$mKey] = $mValue; 
                } 
            } 
        } else { 
            $aReturn[$mKey] = $mValue; 
        } 
    } 

    return $aReturn; 
} 

$arr1 = arrayRecursiveDiff($newentry,$oldentry);

但这只是向我显示新条目,而没有任何比较.希望我已经很好地解释了我的问题.

But it is only showing me the new entry without any comparison. Hope I have explained my problem well enough.

推荐答案

根据我的收集,您正在寻找一种突出显示文本差异以查看描述之间差异的方法.从那里开始,我建议您将diff函数限制为 b_discription ,因为其他属性似乎不在您需要的范围之内.您是否希望更改 business_logo business_name mod_date ?无论如何,如果可以将以下函数转换为字符串,则可以在对象中的所有属性上运行它们.

From what I gathered you're looking at a way to highlight differences in text to see the differences between the descriptions. From there, may I suggest that you limit the diff function to just b_discription as the other attributes don't appear to be within the scope of what you need. Do you expect business_logo, business_name or mod_date to be changed? Regardless, the following function can be run over all attributes in your object if they can be converted to a string.

simplediff 是出色的脚本,可以发现两个字符串之间的差异,包括:

simplediff is an excellent script that finds the difference between two strings, including:

  • 存在于A中,不存在于B中(删除)
  • 存在于B而不是A(插入)
<?php
/*
    Paul's Simple Diff Algorithm v 0.1
    (C) Paul Butler 2007 <http://www.paulbutler.org/>
    May be used and distributed under the zlib/libpng license.
*/
function diff($old, $new){
    $matrix = array();
    $maxlen = 0;
    foreach($old as $oindex => $ovalue){
        $nkeys = array_keys($new, $ovalue);
        foreach($nkeys as $nindex){
            $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
                $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
            if($matrix[$oindex][$nindex] > $maxlen){
                $maxlen = $matrix[$oindex][$nindex];
                $omax = $oindex + 1 - $maxlen;
                $nmax = $nindex + 1 - $maxlen;
            }
        }   
    }
    if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new));
    return array_merge(
        diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
        array_slice($new, $nmax, $maxlen),
        diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
}
function htmlDiff($old, $new){
    $ret = '';
    $diff = diff(preg_split("/[\s]+/", $old), preg_split("/[\s]+/", $new));
    foreach($diff as $k){
        if(is_array($k))
            $ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').
                (!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":'');
        else $ret .= $k . ' ';
    }
    return $ret;
}

$old = "We smsfactory are world's leading SMS messaging provider offering remarakable and reliable SMS Text and Voice messaging globally through almost all-networks of mobile phones successfully. You may contact-us anytime for making any query. Our Services are very useful economically as well as eco-friendly";

$new = "We smsfactory are world's leading SMS messaging provider offering remarakable and reliable SMS Text and Voice messaging globally through almost all-networks of mobile phones successfully. You may contact-us anytime for making any query. Our Services are very useful economically as well as eco-friendly, which enables you to send simultaneous bulk sms to your targeted Customers, Regular-Customers, Buyers, Shoppers, Fans, Regular-shoppers, Clients, Clientele, Members, Managers, Supervisors, Fieldworkers, Graduates, Post graduates, Technicians, Public, Citizens, Mobile-Users, Viewers, Future-purchasers, Users, End-users, Students, Job-seekers, Enjoyers, Visitors, Frequent-visitors, Persons, Individuals, Frequenter, Obtainers, Receivers, Assignees, Recipients, Travelers, Tourists, Guys, Persons, Men and Women, Spectators, Technicians, Staff, Workers, Recruiters, Newcomers, Representatives, Dealers, Distributors, Followers, Shareholders, Investors, Bondholders, Shareowners, Financiers, Bankers, Participants, Associates, Assistants, Colleagues, Contributors, Helpers, Partakers, Party, Sharers, Supporters, Admirers, Devotees, Groupies, Enthusiasts and many more.";

?>
<!doctype html>
<head>
    <style>
        .container {
            width: 50%;
            margin-right: auto;
            margin-left: auto;
            font-family: sans-serif;
            font-size: 12px;
            line-height: 16px;
        }

        del {
            background-color: #FFAB91;
            color: ;
            text-decoration: none;
        }

        ins {
            background-color: #C5E1A5;
            color: ;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <?php echo htmlDiff($old, $new); ?>
    </div>  
</body>
</head>

工作示例: https://3v4l.org/uU0dv

Working Example: https://3v4l.org/uU0dv

这篇关于比较两个不同的多维数组并突出显示更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆