Array ( "concurso" =>-6ren">
gpt4 book ai didi

PHP 警告 : array_filter() expects parameter 2 to be a valid callback, 第二个数组成员不是有效方法

转载 作者:行者123 更新时间:2023-12-04 20:31:07 25 4
gpt4 key购买 nike

我最初有这个大数组,它是从我的数据库中以缩短的形式获得的:

$arreglo =Array
(
"0" => Array
(
"concurso" => 2600,
"R1" => 1
),

"1" => Array
(
"concurso" => 2602,
"R1" => 1
),

"2" => Array
(
"concurso" => 2603,
"R1" => 1
),

"3" => Array
(
"concurso" => 2648,
"R1" => 1
),

"4" => Array
(
"concurso" => 2653,
"R1" => 1
),

"5" => Array
(
"concurso" => 2655,
"R1" => 1
),

"6" => Array
(
"concurso" => 2698,
"R1" => 1
),

"7" => Array
(
"concurso" => 2722,
"R1" => 1
),

"8" => Array
(
"concurso" => 2741,
"R1" => 1
),

"9" => Array
(
"concurso" => 2743,
"R1" => 1
),

"10" => Array
(
"concurso" => 2744,
"R1" => 1
),

"11" => Array
(
"concurso" => 2745,
"R1" => 1
),

"12" => Array
(
"concurso" => 2763,
"R1" => 1
),

"13" => Array
(
"concurso" => 2778,
"R1" => 1
),

"14" => Array
(
"concurso" => 2780,
"R1" => 1
),

"15" => Array
(
"concurso" => 2782,
"R1" => 1
),

"16" => Array
(
"concurso" => 2607,
"R1" => 2
),

"17" => Array
(
"concurso" => 2609,
"R1" => 2
)
);

一直持续到“R1”元素值为 56。

所以我想分开 R1 的每组值,例如,当 R1 等于 1 时,我使用以下函数将“concurso”的每个值存储在一个名为“$concursos”的数组中:

function category($var)
{
return (is_array($var) && $var['R1'] == 1);
}
$current=array_filter($arreglo,"category");

到现在为止一切正常,因为当 R1 = 1 时,我只得到当 R1 = 1 时的 concursos 列表,如下所示:

Array
(
[0] => 2600
[1] => 2602
[2] => 2603
[3] => 2648
[4] => 2653
[5] => 2655
[6] => 2698
[7] => 2722
[8] => 2741
[9] => 2743
[10] => 2744
[11] => 2745
[12] => 2763
[13] => 2778
[14] => 2780
[15] => 2782
)

问题是,如果我想在 for 循环中对 R1=2,3,...56 的以下数字再次进行此操作,那么,我将设置 ==$ 而不是指定 ==1 currentR1,在函数中是这样的:

function category($var)
{
return (is_array($var) && $var['R1'] == $currentR1);
}
$current=array_filter($arreglo,"category");

现在的问题是,如果我尝试输入参数,函数调用失败,我如何在这里指定一个参数?

我试过,例如,

$current=array_filter($repeticiones,array('category',$l));

它没有警告我

警告:array_filter() 期望参数 2 是有效的回调,第二个数组成员不是第 X 行 myScript.php 中的有效方法

那我该如何指定参数呢?

最佳答案

你可以像这样使用匿名函数:

$r = 2;
$current = array_filter($arreglo, function($var) use ($r){
// ^ import variable to the closure scope
return (is_array($var) && $var['R1'] == $r);
});

关于PHP 警告 : array_filter() expects parameter 2 to be a valid callback, 第二个数组成员不是有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26081724/

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