無料スクリプト配布のPHP.TO   PHPの実用的なtips PHPマニュアル MySQLマニュアル Apacheマニュアル PostgreSQLマニュアル マニュアル検索    

array_filter

(PHP 4 >= 4.0.6, PHP 5)

array_filter ???????

array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )

callback ?????? array ????? callback ? true ?? array ???? array ?? ?

??

array

callback

???

???? array ? FALSE ? (boolean ?? ??) ???

flag

?? callback ??

  • ARRAY_FILTER_USE_KEY - ???? callback ??
  • ARRAY_FILTER_USE_BOTH - ?????? callback ??

?

???????

?

5.6.0 ??? flag ? ARRAY_FILTER_USE_KEY ARRAY_FILTER_USE_BOTH ??

1 array_filter()

<?php
function odd($var)
{
    
// ???????
    
return($var 1);
}

function 
even($var)
{
    
// ???????
    
return(!($var 1));
}

$array1 = array("a"=>1"b"=>2"c"=>3"d"=>4"e"=>5);
$array2 = array(6789101112);

echo 
"Odd :\n";
print_r(array_filter($array1"odd"));
echo 
"Even:\n";
print_r(array_filter($array2"even"));
?>

??????

Odd :
Array
(
    [a] => 1
    [c] => 3
    [e] => 5
)
Even:
Array
(
    [0] => 6
    [2] => 8
    [4] => 10
    [6] => 12
)

2 callback ? array_filter()

<?php

$entry 
= array(
             
=> 'foo',
             
=> false,
             
=> -1,
             
=> null,
             
=> ''
          
);

print_r(array_filter($entry));
?>

??????

Array
(
    [0] => foo
    [2] => -1
)

3 array_filter() flag ?

<?php

$arr 
= ['a' => 1'b' => 2'c' => 3'd' => 4];

var_dump(array_filter($arr, function($k) {
    return 
$k == 'b';
}, 
ARRAY_FILTER_USE_KEY));

var_dump(array_filter($arr, function($v$k) {
    return 
$k == 'b' || $v == 4;
}, 
ARRAY_FILTER_USE_BOTH));
?>

??????

array(1) {
  ["b"]=>
  int(2)
}
array(2) {
  ["b"]=>
  int(2)
  ["d"]=>
  int(4)
}

?

???? (??????) ????



powered by SEO.CUG.NET