gpt4 book ai didi

php - 我可以将其他参数传递给内联 usort 函数吗?

转载 作者:行者123 更新时间:2023-12-02 17:13:37 26 4
gpt4 key购买 nike

考虑以下代码:

$external_variable = TRUE;
usort($array, function($a, $b) {
// do sorting stuff based on $external_variable
});

如何在 usort 函数中访问和使用 $external_variable?

最佳答案

使用 use 关键字:

$external_variable = TRUE;

usort($array, function($a, $b) use($external_variable) {
// do sorting stuff based on $external_variable
});

http://php.net/manual/en/functions.anonymous.php

Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct. From PHP 7.1, these variables must not include superglobals, $this, or variables with the same name as a parameter.

关于php - 我可以将其他参数传递给内联 usort 函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47718732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com