I originally have this big array that i get from my database in a shortened version:
$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
)
);
It goes on until the "R1" element value is 56.
So i want to separate each set of values of R1, for example, just when R1 equals 1, i store each value of "concurso" in an array called "$concursos" by using the following function:
function category($var)
{
return (is_array($var) && $var['R1'] == 1);
}
$current=array_filter($arreglo,"category");
Everything works fine until now, since, when R1 = 1, i get only that list of concursos when R1 = 1 as follows:
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
)
The problem is that, if i want to make this again for the following numbers where R1=2,3,...56 inside a for loop, then, instead of specifying ==1, i would set ==$currentR1, in the function like this:
function category($var)
{
return (is_array($var) && $var['R1'] == $currentR1);
}
$current=array_filter($arreglo,"category");
Now the problem is that if i try to put an argument, the call of the function fails, how can i specify here a parameter?
I have tried, for example,
$current=array_filter($repeticiones,array('category',$l));
and it fails warning me
Warning: array_filter() expects parameter 2 to be a valid callback, second array member is not a valid method in myScript.php on line X
Then how can i specify the parameter?
You can use an anonymous function like this:
$r = 2;
$current = array_filter($arreglo, function($var) use ($r){
// ^ import variable to the closure scope
return (is_array($var) && $var['R1'] == $r);
});