可以通过闭包传递给PHP中的usort吗? [英] Possible to pass a closure to usort in PHP?

查看:96
本文介绍了可以通过闭包传递给PHP中的usort吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有如下数组排序功能:

I have an array sorting function as follows:

public function sortAscending($accounts)
{
    function ascending($accountA, $accountB) {
        if ($accountA['AmountUntilNextTarget'] == $accountB['AmountUntilNextTarget']) {
            return 0;
        }
        return ($accountA['AmountUntilNextTarget'] < $accountB['AmountUntilNextTarget']) ? -1 : 1;
    }
    usort($accounts, $ascending);

    return $accounts;
}

很显然,这并不理想,因为它很难对要搜索的密钥进行编码.我以为可以通过将键作为参数传递给外部函数来使之通用,但是这在内部函数中就不适用了.我试图通过使用闭包来解决这个问题,该闭包可以访问该参数,而不是如下所示的内部函数:

Clearly this is not ideal as it is hard-coding the key to search for. I thought I would make this generic by passing the key as a param to outside function, however this is then out-of-scope in the inner function. I tried to get around this by using a closure, which would have access to the param, instead of an inner function as follows:

public function sortAscending($accounts, $key)
{
    $ascending = function($accountA, $accountB) {
        if ($accountsA[$key] == $accountB[$key]) {
            return 0;
        }
        return ($accountA[$key] < $accountB[$key]) ? -1 : 1;
    }
    usort($accounts, $ascending);

    return $accounts;
}

但是usort()只接受一个函数名,因此不起作用.谁能看到(更好的)方法吗?

However usort() only accepts a function name, so this doesn't work. Can anyone see a (better?) way of achieving this?

推荐答案

  • 定义闭包时,可以使用 use 关键字使函数看到"某个变量(或多个变量).另请参见有关匿名函数的PHP文档.
    • When defining closures, you can use the use keyword to let the function "see" a certain variable (or variables). See also the PHP documentation about Anonymous functions.
    • 关闭也可能继承变量来自父范围.任何这样的变量必须在函数头.继承变量从父范围不一样作为使用全局变量.全球的变量存在于全局范围内不管是什么都一样函数正在执行.父母闭包的范围是声明关闭的内容(不是一定是它被调用的功能来自).

      Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header. Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from).

      • 请注意,定义闭包并将其分配给变量是正常的赋值操作,因此在闭包的} 之后需要; .
      • 进行这些更改后,您的代码将如下所示(并且应该可以正常工作):

        After making these changes your code would look like this (and should work fine):

        public function sortAscending($accounts, $key)
        {
            $ascending = function($accountA, $accountB) use ($key) {
                if ($accountsA[$key] == $accountB[$key]) {
                    return 0;
                }
                return ($accountA[$key] < $accountB[$key]) ? -1 : 1;
            };
            usort($accounts, $ascending);
        
            return $accounts;
        }
        

        这篇关于可以通过闭包传递给PHP中的usort吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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