如何传递usort()参数? [英] How to pass usort() a parameter?

查看:141
本文介绍了如何传递usort()参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

function sortx($a, $b) {
    if(!strpos($a["p_title"],'apple ipad')) {
        return -1; 
    }
    return 1;
}
usort($arr, 'sortx');`

在上面的函数中,我想传递: $sort_text='apple ipad'; ,而不是将apple ipad硬编码为strpos()时调用函数.我该怎么做?

In above function I want to pass: $sort_text='apple ipad'; , when calling function instead of hardcoding apple ipad into strpos(). How can I accomplish that?

推荐答案

以闭包形式调用它:

$sort_text='apple ipad';
usort(
    $arr, 
    function ($a, $b) use ($sort_text) {
        if(!strpos($a["p_title"], $sort_text)) {
            return -1; 
        }
        return 1;
    }
);

,您可以在use子句中传递其他参数

and you can pass additional arguments with the use clause

这篇关于如何传递usort()参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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