2013-03-19 61 views
0

我有2個陣列多維數組,檢查差異

Array 
(
[0] => Array 
    (
     [SKU] => 379 
     [ProductName] => Wrap - Black 
     [ProductSellingPrice] => 1.00 
     [ProductQty] => 1 
    ) 

[1] => Array 
    (
     [SKU] => 3909 
     [ProductName] => Wrap - Navy 
     [ProductSellingPrice] => 0.00 
     [ProductQty] => 1 
    ) 

和第二:

Array 
(
[0] => Array 
    (
     [SKU] => 378 
     [ProductName] => Wrap - White 
     [ProductSellingPrice] => 1.00 
     [ProductQty] => 1 
    ) 

[1] => Array 
    (
     [SKU] => 3909 
     [ProductName] => Wrap - Navy 
     [ProductSellingPrice] => 0.00 
     [ProductQty] => 1 
    ) 

我想通過他們循環和返回鍵值之間的差異。

+0

[你嘗試過什麼?](http://www.whathaveyoutried.com/)參見[問諮詢】(http://stackoverflow.com/questions/ask-advice),請。 – 2013-03-19 16:15:18

回答

2

我前一段時間寫過這個,只是想問你在問什麼。 「compare_records」方法返回一個由字段名稱鍵入的關聯變更數組。每個陣列元件是具有以下項的數組:

  1. 從:原始(左)值
  2. 於:新的(右)值
  3. 消息:日誌消息
  4. 事件:描述的性質換作添加,刪除或更改

這樣稱呼它:

$changes = db_tools::compare_records($array_old, $array_new); 

class db_tools 
{ 
    public static $default_log_format = 'Field %1$s changed from "%2$s" to "%3$s"'; 

/** 
* Returns a sorted list of keys from two arrays 
**/ 
    public static function get_keys(&$a, &$b) 
    { 
     $keys = array(); 
     foreach($a as $k => $v) $keys[$k] = 1; 
     foreach($b as $k => $v) $keys[$k] = 1; 
     ksort($keys); 
     return array_keys($keys); 
    } // get_keys 


/** 
* Compares values in two arrays and returns array of differences. 
* 
* Each difference element is an associative array in the following format: 
*  'field/key name' => array(
*   'from'  => "from_val" 
*  , 'to'  => "to_val" 
*  , 'message' => "Loggable message" 
*  , 'event'  => "add" or "remove" or "change" 
*  ) 
* 
* @param Array Original (old) array 
* @param Array Comparison (new) array 
* @param Array Special handling instructions -- allows you to ignore fields 
*    or truncate the logged value, as in: 
*    array(
*     'field_name' => 'ignore' // do not detect changes 
*    , 'field_2' => 0 // detect changes, but do not describe them 
*    , 'field_3' => 'from %2$s to %3s field %1$s was changed' // specially formatted log message 
*    ); 
* @param Array List of keys to compare. If this is omitted, the list will be 
*    constructed from keys in the first two arguments. 
* @return Array 
* @author J.D. Pace 
* @since 2010/10/02 
**/ 
    public static function compare_records(&$a, &$b, $special = array(), $keyset = null) 
    { 
     $keys = (empty($keyset) ? self::get_keys($a, $b) : $keyset); 
     $diff = array(); 

     foreach($keys as $k) 
     { 
      $_a = (array_key_exists($k, $a) ? trim($a[$k]) : ''); 
      $_b = (array_key_exists($k, $b) ? trim($b[$k]) : ''); 

      if($_a != $_b) 
      { 
       $fmt = &self::$default_log_format; 
       if(array_key_exists($k, $special)) 
       { 
        if($special[$k] == 'ignore') continue; 

        if($special[$k] == '0')  $fmt = '%1$s changed'; 
        else       $fmt = &$instr; 

       } 
       $diff[$k] = array(
        'from'  => $_a 
        , 'to'  => $_b 
        , 'message' => sprintf($fmt, $k, $_a, $_b) 
        , 'event'  => (
         empty($_a) 
         ? 'add' 
         : (
          empty($_b) 
          ? 'remove' 
          : 'change' 
         ) 
        ) 
       ); 
      } 
     } 
     return $diff; 
    } 

/** 
* Applies new data from record b to record a according to options keyset 
* @param Array Destination Array 
* @param Array Source Array 
* @param Array Keys to apply. If this is omitted, record b will be copied 
*    into record a 
**/ 
    public static function update_record(&$a, &$b, $keyset = null) 
    { 
     $keys = (empty($keyset) ? self::get_keys($a, $b) : $keyset); 
     $updated = array(); 
     foreach($keys as $k) 
     { 
      $a[$k] = (empty($b[$k]) ? '' : $b[$k]); 
     } 
    } 

} // class db_tools 
+0

Pace當試圖運行這個時,我有一個錯誤:'trim()期望參數1是字符串' – 2014-03-29 19:51:29

+0

@beginningperl這段代碼希望這兩個數組都是單維的。一個數組的元素是否可能是另一個數組? – 2014-03-31 16:17:52