簡體   English   中英

比較兩個不同的多維數組並突出顯示更改

[英]Compare two different multidimentional arrays and highlight the changes

我正在創建一個日志文件,該文件中我想同時顯示舊條目和更新條目,但是與此同時,我也想突出顯示新條目的更改,就像在stackoverflow中所做的那樣,當我們編輯問題時,它們突出顯示了編輯部分。

如您所見,描述有所不同,並且還有許多其他索引,我只展示其中一些。

我有兩個不同的數組,如下所示:

新條目

   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
        )

)

我對此進行了研究,發現了很多功能,從中可以嘗試以下功能:

 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);

但這只是向我顯示新條目,而沒有任何比較。 希望我已經很好地解釋了我的問題。

根據我的收集,您正在尋找一種突出顯示文本差異以查看描述之間差異的方法。 從那里開始,我可能建議您將diff函數限制為b_discription因為其他屬性似乎不在您需要的范圍之內。 您是否希望business_logobusiness_namemod_date更改? 無論如何,如果可以將以下函數轉換為字符串,則可以在對象中的所有屬性上運行它們。

simplediff是一個出色的腳本,可以發現兩個字符串之間的差異,包括:

  • 存在於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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM