2016-07-05 32 views
0

我創建一個日誌文件,在其中我想顯示舊條目和更新的條目都在一起,但沿着這些我也想強調在新條目的變化,喜歡它的當我們編輯他們突出顯示編輯過的部分的問題時,在計算器中完成。比較兩個不同的多維數組和突出的變化

正如你可以看到有在描述的不同,還有許多其他的指標,我只是展示其中的一些。

我有一個看起來像這樣兩個不同的陣列:

新條目

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

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

+0

哪些屬性是你期待有所不同,'只有b_discription'或整個對象? [這個類提供了一個差異比較](http://code.stephenmorley.org/php/diff-implementation/),就像你會遇到源代碼修訂系統一樣。 – mulquin

+0

我想比較整個數組值,所以我可以突出顯示新 –

+0

是類似的東西,你顯示的鏈接 –

回答

0

從我收集你正在尋找一種方式來突出顯示文本的差異看描述之間的差異。從那裏,我可能會建議你將diff函數限制爲b_discription,因爲其他屬性看起來不在你需要的範圍內。你期望business_logobusiness_namemod_date被更改?無論如何,如果可以將對象中的所有屬性轉換爲字符串,那麼可以運行以下函數。

simplediff是查找兩個字符串之間的差,包括優異的腳本:

  • 存在於一個,而不是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

+0

好非常感謝好友數組值這個腳本是真棒它的變化節省我很多時間,謝謝:) –

+0

嗨@mulquin你能告訴我,它是否比較'html'標籤嗎? –