{"id":851,"date":"2018-01-10T13:00:43","date_gmt":"2018-01-10T04:00:43","guid":{"rendered":"http:\/\/gomocool.net\/gomokulog\/?p=851"},"modified":"2018-01-10T13:00:43","modified_gmt":"2018-01-10T04:00:43","slug":"php-array_filter-%e3%81%a8%e3%81%af","status":"publish","type":"post","link":"https:\/\/gomokulog.gomocool.net\/?p=851","title":{"rendered":"[PHP] array_filter \u3068\u306f"},"content":{"rendered":"<p>\u899a\u3048\u306a\u304f\u3066\u3082\u3044\u3044\u304c\u3001\u899a\u3048\u308b\u3068\u5c11\u3057\u30af\u30fc\u30eb\u306b\u66f8\u3051\u308b array_filter \u3002<\/p>\n<p>PHP\u306e\u4ed5\u69d8\u3060\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:php decode:true \">array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )<\/pre>\n<p>array_fileter\u306f\u3001\u914d\u5217\u306b\u6761\u4ef6\u3092\u4e0e\u3048\u3066\u3001\u7d5e\u308a\u8fbc\u3093\u3060\u914d\u5217\u3092\u8fd4\u3059\u306e\u304c\u76ee\u7684\u3067\u3059\u3002<br \/>\n\u306a\u306e\u3067\u3001\u6700\u521d\u306e\u914d\u5217\u306e\u4e2d\u306b\u5165\u3063\u3066\u3044\u308b\u3082\u306e\u3068\u9055\u3046\u3082\u306e\u304c\u8fd4\u3063\u3066\u304f\u308b\u3053\u3068\u306f\u7121\u3044\u3067\u3059\u3002<\/p>\n<h2>\u7c21\u5358\u306a\u4f7f\u3044\u65b9\u3002<\/h2>\n<p>0\u3088\u308a\u5927\u304d\u3044\u6570\u5b57\u306e\u307f\u306e\u914d\u5217\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:php decode:true\">$simpleArr = [1, 2, -1, 3, 0 -4];\r\n\r\n$tmpArr = array_filter($simpleArr, 'higherThanZero');\r\nprint_r($tmpArr);\r\n\r\nfunction higherThanZero($val){\r\n\tif($val &gt; 0) {\r\n\t\treturn true;\r\n\t}\r\n}<\/pre>\n<p>\u51fa\u529b<\/p>\n<pre class=\"lang:default decode:true\">Array\r\n(\r\n    [0] =&gt; 1\r\n    [1] =&gt; 2\r\n    [3] =&gt; 3\r\n)\r\n<\/pre>\n<p>\u7b2c1\u5f15\u6570\u306b\u914d\u5217\u3001\u7b2c2\u5f15\u6570\u306b\u95a2\u6570\u540d\u3002<\/p>\n<p>\u95a2\u6570\u306e\u5f15\u6570\u306b\u6b21\u3005\u3068\u914d\u5217\u306e\u4e2d\u8eab\u304c\u6e21\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u6761\u4ef6\u306etrue\u304c\u8fd4\u3063\u305f\u3082\u306e\u3060\u3051\u304c\u3001\u914d\u5217\u306b\u6b8b\u308a\u307e\u3059\u3002<\/p>\n<h2>foreach\u3060\u3068<\/h2>\n<p>array_filter\u306fforeach\u306b\u7f6e\u304d\u63db\u3048\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:php decode:true \">$tmpArr = [];\r\nforeach ($simpleArr as $key=&gt;$val){\r\n\tif($val &gt; 0){\r\n\t\t$tmpArr[$key] = $val;\r\n\t}\r\n}\r\nprint_r($tmpArr);<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u533f\u540d\u95a2\u6570\u3067\u8a18\u8ff0\u3059\u308b<\/h2>\n<p>\u4eca\u306e\u4f8b\u3060\u3068\u3001foreach\u306e\u307b\u3046\u304c\u77ed\u304f\u3066\u5206\u304b\u308a\u3084\u3059\u3044\u6c17\u304c\u3057\u307e\u3059\u3002<\/p>\n<p>\u305d\u3053\u3067\u3001\u4e00\u5ea6\u3057\u304b\u4f7f\u308f\u306a\u3044\u304b\u3082\u3057\u308c\u306a\u3044\u95a2\u6570\u3092\u5ba3\u8a00\u305b\u305a\u306b\u3001\u533f\u540d\u95a2\u6570\u3068\u3057\u3066\u305d\u306e\u5834\u3067\u5ba3\u8a00\u3057\u305f\u66f8\u304d\u65b9\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:php decode:true \">$tmpArr = array_filter($simpleArr, function($var){\r\n\tif($var &gt; 0) {\r\n\t\treturn true;\r\n\t}\r\n});\r\nprint_r($tmpArr);<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u30ad\u30fc\u3082\u6e21\u3059[PHP5.6\u4ee5\u4e0a]<\/h2>\n<p>\u6761\u4ef6\u5f0f\u306e\u5f15\u6570\u306b\u5024\u3057\u304b\u6e21\u3057\u3066\u3044\u307e\u305b\u3093\u304c\u3001\u4f8b\u3048\u3070\u3001\u9023\u60f3\u914d\u5217\u306e\u30ad\u30fc\u3067\u5224\u65ad\u3057\u305f\u3044\u5834\u5408\u306a\u3069\u306f\u3001\u7b2c3\u306e\u5f15\u6570\u306b\u3001ARRAY_FILTER_USE_BOTH \u3092\u8ffd\u8a18\u3057\u307e\u3059\u3002<br \/>\n(PHP5.6\u4ee5\u964d\u3067\u306a\u3044\u3068\u4f7f\u3048\u307e\u305b\u3093)<\/p>\n<pre class=\"lang:php decode:true \">$simpleArr = ['banana'=&gt;1, 'apple'=&gt; 2, 'strawberry'=&gt;0, 'pineapple'=&gt;4];\r\n\r\n$tmpArr = array_filter($simpleArr, function($val, $key){\r\n\tif($key == 'banana' || $key == 'strawberry') {\r\n\t\tif($val &gt; 0){\r\n\t\t\treturn true;\r\n\t\t}\r\n\t}\r\n}, ARRAY_FILTER_USE_BOTH);\r\nprint_r($tmpArr);<\/pre>\n<p>\u4e0a\u306e\u4f8b\u306f\u3001banana\u304bstrawberry\u306e\u3046\u3061\u30010\u3088\u308a\u5927\u304d\u3044\u6570\u5b57\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">Array\r\n(\r\n    [banana] =&gt; 1\r\n)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u3042\u3068\u3001\u3042\u307e\u308a\u4f7f\u308f\u306a\u3044\u6c17\u304c\u3057\u307e\u3059\u304c\u3001ARRAY_FILTER_USE_KEY \u306b\u3059\u308b\u3068\u3001\u5f15\u6570\u306b\u30ad\u30fc\u3060\u3051\u3092\u6e21\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:php decode:true\">$simpleArr = ['banana'=&gt;1, 'apple'=&gt; 2, 'strawberry'=&gt;0, 'pineapple'=&gt;4];\r\n\r\n$tmpArr = array_filter($simpleArr, function($key){\r\n\tif($key == 'banana' || $key == 'strawberry') {\r\n\t\treturn true;\r\n\t}\r\n}, ARRAY_FILTER_USE_KEY);\r\nprint_r($tmpArr);<\/pre>\n<p>&nbsp;<\/p>\n<h2>array_values\u3067\u30ad\u30fc\u3092\u4e26\u3073\u66ff\u3048\u308b\u3002<\/h2>\n<p>\u304a\u6c17\u3065\u304d\u306e\u901a\u308a\u3001array_fileter\u306f\u30ad\u30fc\u3092\u4fdd\u6301\u3057\u307e\u3059\u3002<\/p>\n<p>\u9023\u60f3\u914d\u5217\u306a\u3069\u306f\u3001\u30ad\u30fc\u3092\u4fdd\u6301\u3059\u308b\u306e\u306b\u90fd\u5408\u304c\u3044\u3044\u306e\u3067\u3059\u304c\u3001\u914d\u5217\u3060\u3068\u3001\u30ad\u30fc\u3092\u518d\u5ea6\u632f\u308a\u76f4\u3057\u305f\u3044\u5834\u5408\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u5834\u5408\u306f\u3001array_balues\u3067\u914d\u5217\u3092\u4f5c\u308a\u76f4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:php decode:true \">$simpleArr = [1, 2, -1, 3, 0 -4];\r\n\r\n$tmpArr = array_filter($simpleArr, function($var){\r\n\tif($var &gt; 0) {\r\n\t\treturn true;\r\n\t}\r\n});\r\n$tmpArr = array_values($tmpArr);\r\nprint_r($tmpArr);<\/pre>\n<p>\u5148\u307b\u3069\u306f\u30010, 1, 3 \u3068\u306a\u3063\u3066\u3044\u305f\u306e\u304c\u3001\u30ad\u30fc\u304c\u8a70\u3081\u3089\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:php decode:true \">Array\r\n(\r\n    [0] =&gt; 1\r\n    [1] =&gt; 2\r\n    [2] =&gt; 3\r\n)<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u6b63\u76f4\u3001\u666e\u901a\u306eforeach\u3067\u5341\u5206\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>array_filter\u3088\u308a\u3001foreach\u306e\u307b\u3046\u304c\u3001\u95a2\u6570\u547c\u3073\u51fa\u3057\u304c\u7121\u3044\u306e\u3067\u901f\u5ea6\u3082\u901f\u304f\u3001PHP\u306e\u719f\u7df4\u8005\u3067\u306a\u304f\u3066\u3082\u8aad\u3081\u308b\u306e\u3067\u3002<\/p>\n<p>\u3058\u3083\u3041\u3001\u3044\u3064\u4f7f\u3046\u304b\u3002<\/p>\n<p>foreach\u3088\u308a\u8aad\u307f\u3084\u3059\u3044\u3068\u611f\u3058\u305f\u5834\u6240\u3067\u4f7f\u3046\u306e\u304b\u306a\u30fb\u30fb\u30fb\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u899a\u3048\u306a\u304f\u3066\u3082\u3044\u3044\u304c\u3001\u899a\u3048\u308b\u3068\u5c11\u3057\u30af\u30fc\u30eb\u306b\u66f8\u3051\u308b array_filter \u3002 PHP\u306e\u4ed5\u69d8\u3060\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u3002 array array_filter ( array $array [, callable $c&hellip;  <a href=\"https:\/\/gomokulog.gomocool.net\/?p=851\" class=\"more-link\">more <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-851","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts\/851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=851"}],"version-history":[{"count":3,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts\/851\/revisions"}],"predecessor-version":[{"id":854,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts\/851\/revisions\/854"}],"wp:attachment":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}