@pathros는 : OK 미안
//Define your array
$my_array = array(
0 => array(
'cat' => '1',
'value' => 'Value A'
)
,
1 => array(
'cat' => '2',
'value' => 'Value B'
)
,
2 => array(
'cat' => '0',
'value' => 'Value C'
)
,
3 => array(
'cat' => '1',
'value' => 'Value D'
)
);
//Define your filtering function
function my_filtering_function($in_array) {
return is_array($in_array) && $in_array['cat'] == $GLOBALS['filter_param_1'];
}
//TEST #1 : Set the desired value to 2
$GLOBALS['filter_param_1'] = 2;
//Filter your array to only return items that match "cat=2"
$filtered_array = array_filter($my_array, 'my_filtering_function');
e('Number of matching records : '.count($filtered_array)).'record(s)<br>'; //Will return "1 record(s)" (the second record of your array)
//TEST #2 : Set the desired value to 1
$GLOBALS['filter_param_1'] = 1;
//Filter your array to only return items that match "cat=1"
$filtered_array = array_filter($my_array, 'my_filtering_function');
e('Number of matching records : '.count($filtered_array)).'record(s)<br>'; //Will return "2 record(s)" (the first and the last of your array)
(테스트 완료 :-))
필터링에 대한 다른 값을 사용하는 솔루션은 다음과 같다. 그것의 작동 감사 –
나는 질문을 어떻게 변수에 번호 (이 경우 1) 설정할 수 있습니까? 나는이 함수를 for 루프 내에서 1을 변경하는 곳에 적용하고자한다. 항상 1이되는 것은 아니다. $ var [ 'category'] == $ number); 그래서 어떻게 인수를 사용하여이 함수를 호출합니까? 그렇지 않으면, 경고를 얻습니다 array_filter()는 유효한 콜백이 될 매개 변수 2를 기대합니다 – Pathros