多维数组,检查差异 [英] Multidimensional Arrays, check difference

查看:54
本文介绍了多维数组,检查差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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
    )

我想遍历它们并返回键值之间的差.

I would like to loop through them and return the difference between key values.

推荐答案

我前一阵子写了这篇文章来做您要问的事情. "compare_records"方法返回一个关联的更改数组,以字段名称作为关键字.每个数组元素都是一个包含以下各项的数组:

I wrote this a while ago to do just what you're asking. The "compare_records" method returns an associative array of changes, keyed by field name. Each array element is an array with the following items:

  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

这篇关于多维数组,检查差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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