myclabs/array-comparator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

myclabs/array-comparator

最新稳定版本:1.1.0

Composer 安装命令:

composer require myclabs/array-comparator

包简介

Array comparator

README 文档

README

Array comparison helper.

Latest Stable Version Build Status Coverage Status

Principle

Here is the default behavior:

Array 1 Array 2 Method called
foo => Foo foo => Foo whenEqual
bar => Bar bar => Foo whenDifferent
baz => Baz whenMissingRight
bam => Bam whenMissingLeft

By default, array keys are compared. This behavior can be customized.

Usage

$comparator = new ArrayComparator();

$comparator->whenEqual(function ($item1, $item2) {
    // Do your stuff !
})
->whenDifferent(function ($item1, $item2) {
    // Do your stuff !
})
->whenMissingRight(function ($item1) {
    // Do your stuff !
})
->whenMissingLeft(function ($item2) {
    // Do your stuff !
});

$comparator->compare($array1, $array2);

Advanced example for Doctrine entities for example:

$comparator = new ArrayComparator();

// Set that items are considered the same if they have the same id
// Array keys are ignored in this example
$comparator->setItemIdentityComparator(function ($key1, $key2, $item1, $item2) {
    return $item1->id === $item2->id;
});

// Items have differences if their name differ
$comparator->setItemComparator(function ($item1, $item2) {
    return $item1->name === $item2->name;
});

$comparator->whenEqual(function ($item1, $item2) {
    // Do your stuff !
})
->whenDifferent(function ($item1, $item2) {
    // Do your stuff !
})
->whenMissingRight(function ($item1) {
    // Do your stuff !
})
->whenMissingLeft(function ($item2) {
    // Do your stuff !
});

$comparator->compare($array1, $array2);

Note that you can also use any PHP callable format instead of inline functions, for example:

$comparator->whenDifferent(array($this, 'whenDifferent'));

Documentation

  • whenEqual - Called when 2 items are found in both arrays, and are equal
$comparator->whenEqual(function ($item1, $item2) {
});
  • whenDifferent - Called when 2 items are found in both arrays, but are different
$comparator->whenDifferent(function ($item1, $item2) {
});
  • whenMissingRight - Called when an item is in the first array (left array) but not in the second (right array)
$comparator->whenMissingRight(function ($item1) {
});
  • whenMissingLeft - Called when an item is in the second array (right array) but not in the first (left array)
$comparator->whenMissingLeft(function ($item2) {
});
  • setItemIdentityComparator - Overrides the default identity comparator which determine if 2 items represent the same thing

Can be used for example to compare the id of the items.

The default behavior compares the array keys using ===.

$comparator->setItemIdentityComparator(function ($key1, $key2, $item1, $item2) {
    // return true or false
});
  • setItemComparator - Overrides the default item comparator to determine if 2 items (representing the same thing) have differences

Can be used for example to compare specific attributes of the items. The function should return "is equal", i.e. true if items have no differences (then nothing is done because all is good), or false if they have differences (then whenDifferent is called).

The default behavior compares the items using ==.

$comparator->setItemComparator(function ($item1, $item2) {
    // return true or false
});

Custom comparator

There is an alternative to using setItemIdentityComparator and setItemComparator by writing your own comparator class:

class CustomComparator extends ArrayComparator
{
    protected function areSame($key1, $key2, $item1, $item2)
    {
        // Your stuff
        return $item1->id === $item2->id;
    }

    protected function areEqual($item1, $item2)
    {
        // Your stuff
        return $item1->name === $item2->name;
    }
}

Installation

Edit your composer.json to add the dependency:

{
	"require": {
		"myclabs/array-comparator": "1.0.*"
	}
}

ArrayComparator is tested with PHP 5.3 and greater.

Changelog

  • 1.0: Stable version after testing and use in production (no change from 0.3)
  • 0.3: PHP 5.3 compatibility and support all PHP callable types
  • 0.2: Allowed to extend the ArrayComparator class and write custom comparators
  • 0.1: First version

Contribute

Install dependencies with Composer:

composer install

TODO:

  • Optimize the array traversals
  • Improve documentation ?

统计信息

  • 总下载量: 164.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 20
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 19
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固