PHP 提供了一个闭包来确定数组中项目的唯一性

发布于 2024-09-16 11:09:50 字数 192 浏览 0 评论 0原文

简单的问题:有没有一种方法可以在 PHP 中为 array_unique 函数的某个等效函数提供一个闭包,以便您可以在比较数组中的两个项目时指定要使用的自己的比较闭包?我有一个类实例数组(可能包含重复项),并且想要告诉 PHP 使用特定逻辑来确定唯一性。

PHP 使用 usort() 方法提供了排序功能 - 只是想知道它是否也可用于唯一性检查。谢谢!

Quick question: is there a way to provide a closure in PHP to some equivalent function to the array_unique function so that you can specify your own comparison closure to be used when comparing two items in the array? I have an array of class instances (which may contain duplicates) and want to tell PHP to use particular logic to determine uniqueness.

PHP provides this with sorting using the usort() method - just wondering if it is also available for uniqueness checks. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〃安静 2024-09-23 11:09:50

array_filter 可以对数组中的每个元素应用回调,并返回 true/false 是否将该值保留在返回数组中。 这是一条注释,使用 array_filter 删除重复项一个数组。

there is array_filter that you can apply a callback to each element in an array and return true/false of whether to keep that value in the returning array. Here is a comment using array_filter to remove duplicates in an array.

江南月 2024-09-23 11:09:50

我找不到你正在寻找的确切内容,但我想编写你自己的函数也许不会太难......

$a = new StdClass;
$b = new StdClass;
$c = new StdClass;
$d = new StdClass;

$a->a = 1;
$b->a = 1;
$c->c = 1;
$d->c = 1;

$objects = array( $a,$b,$c,$d );

function custom_array_unique( array $objects ) {

    foreach( $objects as $k =>$object ) {
        foreach( $objects as $k2 => $object2 ) {

            if ( $k !== $k2 && $object == $object2 ) {
                unset( $objects[$k] );
            }

        }
    }
    return $objects;
}

print_r( custom_array_unique($objects));


Array
(
    [1] => stdClass Object
        (
            [a] => 1
        )

    [3] => stdClass Object
        (
            [c] => 1
        )

)

I couldn't find exactly what you are looking for but i thought maybe it wouldn't be too tough to write your own function...

$a = new StdClass;
$b = new StdClass;
$c = new StdClass;
$d = new StdClass;

$a->a = 1;
$b->a = 1;
$c->c = 1;
$d->c = 1;

$objects = array( $a,$b,$c,$d );

function custom_array_unique( array $objects ) {

    foreach( $objects as $k =>$object ) {
        foreach( $objects as $k2 => $object2 ) {

            if ( $k !== $k2 && $object == $object2 ) {
                unset( $objects[$k] );
            }

        }
    }
    return $objects;
}

print_r( custom_array_unique($objects));


Array
(
    [1] => stdClass Object
        (
            [a] => 1
        )

    [3] => stdClass Object
        (
            [c] => 1
        )

)
征棹 2024-09-23 11:09:50

array_unique()手册页 code> 不提供任何指向回调版本的链接,并且左侧链接列表中没有名为 array_uunique() 的函数(如果是这样的话,应该调用该函数)它遵循其他数组排序函数的命名约定 - 但 PHP 在函数命名约定方面并不是很可靠)。

您可以使用双 foreach 循环自行添加此功能:

$uniqueness_fails = false;
foreach ( $myarray as $keyA => $valueA ) {
    foreach ( $myarray as $keyB => $valueB ) {
        if ( $keyA != $keyB and my_equality_function($valueA, $valueB) ) {
            $uniqueness_fails = true;
            break 2;
        }
    }
}

The manual page for array_unique() doesn't provide any links to a callback version, and there isn't a function in the list of links on the left called array_uunique() (which is what such a function should be called if it follows the naming convention of the other array sorting functions - but then PHP isn't very reliable when it comes to function naming conventions).

You could add this functionality yourself using a double foreach loop:

$uniqueness_fails = false;
foreach ( $myarray as $keyA => $valueA ) {
    foreach ( $myarray as $keyB => $valueB ) {
        if ( $keyA != $keyB and my_equality_function($valueA, $valueB) ) {
            $uniqueness_fails = true;
            break 2;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文