<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.filter.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'function.filter-input-array.php',
    1 => 'filter_input_array',
    2 => '外部から変数を受け取り、オプションでそれらをフィルタリングする',
  ),
  'up' => 
  array (
    0 => 'ref.filter.php',
    1 => 'Filter 関数',
  ),
  'prev' => 
  array (
    0 => 'function.filter-input.php',
    1 => 'filter_input',
  ),
  'next' => 
  array (
    0 => 'function.filter-list.php',
    1 => 'filter_list',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'reference/filter/functions/filter-input-array.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.filter-input-array" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">filter_input_array</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">filter_input_array</span> &mdash; <span class="dc-title">外部から変数を受け取り、オプションでそれらをフィルタリングする</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.filter-input-array-description">
  <h3 class="title">説明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">function</span> <span class="methodname"><strong>filter_input_array</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$type</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.integer.php" class="type int">int</a></span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code><a href="filter.constants.sanitization.php#constant.filter-default">FILTER_DEFAULT</a></code></strong></span></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$add_empty</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></span></span>): <span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span></div>

  <p class="para rdfs-comment">
   この関数を使用すると、大量のデータを取得する際に
   <span class="function"><a href="function.filter-input.php" class="function">filter_input()</a></span> を繰り返しコールする必要がなくなるので便利です。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.filter-input-array-parameters">
  <h3 class="title">パラメータ</h3>
  <dl>
   
    <dt><code class="parameter">type</code></dt>
    <dd>
     <p class="simpara">
      定数
      <strong><code><a href="filter.constants.input.php#constant.input-post">INPUT_<span class="replaceable">*</span></a></code></strong>
      のうちのひとつ。
     </p>
     <div class="warning"><strong class="warning">警告</strong>
      <p class="simpara">
       フィルタされるスーパーグローバルの内容は、
       ユーザーがスーパーグローバルに手を加える前の、
       <abbr title="Server Application Programming Interface">SAPI</abbr> が提供する &quot;生の&quot; ものです。
       変更されたスーパーグローバルの値をフィルタするには、
       <span class="function"><a href="function.filter-var-array.php" class="function">filter_var_array()</a></span> を代わりに使います。
      </p>
     </div>
    </dd>
   
   
    <dt><code class="parameter">options</code></dt>
    <dd>
     <p class="simpara">
      オプションの連想配列、または、エントリごとに適用するフィルタ。
      フィルタは検証フィルタ
      <strong><code><a href="filter.constants.validation.php#constant.filter-validate-bool">FILTER_VALIDATE_<span class="replaceable">*</span></a></code></strong>
      または除去フィルタ
      <strong><code><a href="filter.constants.sanitization.php#constant.filter-sanitize-string">FILTER_SANITIZE_<span class="replaceable">*</span></a></code></strong>
      が使えます。
     </p>
     <p class="simpara">
      オプションの配列は、
      入力配列に存在するキーをキーとして持つ連想配列で、
      キーに対応する値には、そのエントリに適用するフィルタ、
      または、そのエントリにどのフィルタを、
      どのように適用するかを記述した連想配列のいずれかを指定します。
     </p>
     <p class="simpara">
      どのようにフィルタを適用するかを連想配列に記述する場合、
      どのフィルタを対応する値に適用するかを <code class="literal">&#039;filter&#039;</code>
      キーに含めなければいけません。
      <code class="literal">&#039;filter&#039;</code> キーに指定できるのは、
      <strong><code><a href="filter.constants.validation.php#constant.filter-validate-bool">FILTER_VALIDATE_<span class="replaceable">*</span></a></code></strong>,
      <strong><code><a href="filter.constants.sanitization.php#constant.filter-sanitize-string">FILTER_SANITIZE_<span class="replaceable">*</span></a></code></strong>,
      <strong><code><a href="filter.constants.sanitization.php#constant.filter-unsafe-raw">FILTER_UNSAFE_RAW</a></code></strong>,
      <strong><code><a href="filter.constants.callback.php#constant.filter-callback">FILTER_CALLBACK</a></code></strong> のうちのいずれかです。
      オプションで <code class="literal">&#039;flags&#039;</code> キーには、
      フィルタに適用するフラグを指定できますし、
      <code class="literal">&#039;options&#039;</code> には、
      フィルタに適用するオプションを指定できます。
     </p>
    </dd>
   
   
    <dt><code class="parameter">add_empty</code></dt>
    <dd>
     <p class="para">
      返り値の配列に対して、キーが存在しない場合に <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> を追加します。
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.filter-input-array-returnvalues">
  <h3 class="title">戻り値</h3>
  <p class="simpara">
   成功した場合は要求された変数の値を含む配列。
  </p>
  <p class="simpara">
   失敗した場合、<strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> を返します。
   <code class="parameter">type</code> で指定された入力配列に値が入っていない場合は、
   代わりに <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> を返します。
  </p>
  <p class="simpara">
   入力配列に欠けているエントリは、
   <code class="parameter">add_empty</code> が <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> の場合は返される配列に <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> として追加され、
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> の場合は一切含まれません。
   <span class="function"><a href="function.filter-input.php" class="function">filter_input()</a></span> とは異なり、
   <strong><code><a href="filter.constants.flags.generic.php#constant.filter-null-on-failure">FILTER_NULL_ON_FAILURE</a></code></strong> フラグを使ってもこの動作は変わらず、
   欠けているエントリは常に <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> になります。
  </p>
  <p class="simpara">
   フィルタが失敗した場合、返される配列のエントリは <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> になります。
   但し、<strong><code><a href="filter.constants.flags.generic.php#constant.filter-null-on-failure">FILTER_NULL_ON_FAILURE</a></code></strong> フラグを使った場合は <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> になります。
   <strong><code><a href="filter.constants.flags.generic.php#constant.filter-force-array">FILTER_FORCE_ARRAY</a></code></strong> フラグを使った場合は、
   この失敗時の値も、ほかの結果と同様に要素を一つ持つ配列でラップされます。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.filter-input-array-examples">
  <h3 class="title">例</h3>
  <div class="example" id="example-1">
   <p><strong>例1 <span class="function"><strong>filter_input_array()</strong></span> の例</strong></p>
   <div class="example-contents"><p>
    この例では、
    <code class="literal">?email=user@example.com&amp;age=twenty&amp;url=https://example.com</code>
    への GET リクエストを想定しています。
    <code class="literal">twenty</code> は整数ではないため、
    <code class="literal">age</code> エントリのフィルタは失敗します。
    <code class="literal">1</code> から <code class="literal">120</code>
    の範囲外の値の場合も、同じように失敗します。
   </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$filters </span><span style="color: #007700">= [
    </span><span style="color: #DD0000">'email' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_VALIDATE_EMAIL</span><span style="color: #007700">,
    </span><span style="color: #DD0000">'age'   </span><span style="color: #007700">=&gt; [
        </span><span style="color: #DD0000">'filter'  </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'options' </span><span style="color: #007700">=&gt; [</span><span style="color: #DD0000">'min_range' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'max_range' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">120</span><span style="color: #007700">],
    ],
    </span><span style="color: #DD0000">'url'   </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_VALIDATE_URL</span><span style="color: #007700">,
];

</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">filter_input_array</span><span style="color: #007700">(</span><span style="color: #0000BB">INPUT_GET</span><span style="color: #007700">, </span><span style="color: #0000BB">$filters</span><span style="color: #007700">);

</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>上の例の出力は、
たとえば以下のようになります。</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(3) {
  [&quot;email&quot;]=&gt;
  string(16) &quot;user@example.com&quot;
  [&quot;age&quot;]=&gt;
  bool(false)
  [&quot;url&quot;]=&gt;
  string(19) &quot;https://example.com&quot;
}</pre>
</div>
   </div>
  </div>
  <div class="example" id="example-2">
   <p><strong>例2 <span class="function"><strong>filter_input_array()</strong></span> で POST データをフィルタリングする</strong></p>
   <div class="example-contents"><p>
    この例では、
    <code class="literal">username=&lt;script&gt;alert&lt;/script&gt;</code> と
    <code class="literal">comment=Hello World</code>
    というフィールドを持つ POST リクエストを想定しています。
    <code class="literal">missing</code> フィールドは送信されていませんが、
    <code class="parameter">add_empty</code> のデフォルトは <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> なので、
    結果には <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> として含まれます。
   </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$filters </span><span style="color: #007700">= [
    </span><span style="color: #DD0000">'username' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_SANITIZE_SPECIAL_CHARS</span><span style="color: #007700">,
    </span><span style="color: #DD0000">'comment'  </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_SANITIZE_SPECIAL_CHARS</span><span style="color: #007700">,
    </span><span style="color: #DD0000">'missing'  </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,
];

</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">filter_input_array</span><span style="color: #007700">(</span><span style="color: #0000BB">INPUT_POST</span><span style="color: #007700">, </span><span style="color: #0000BB">$filters</span><span style="color: #007700">);

</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>上の例の出力は、
たとえば以下のようになります。</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(3) {
  [&quot;username&quot;]=&gt;
  string(38) &quot;&amp;#60;script&amp;#62;alert&amp;#60;/script&amp;#62;&quot;
  [&quot;comment&quot;]=&gt;
  string(11) &quot;Hello World&quot;
  [&quot;missing&quot;]=&gt;
  NULL
}</pre>
</div>
   </div>
  </div>
  <div class="example" id="example-3">
   <p><strong>例3 値が入っていない入力の種類を指定する</strong></p>
   <div class="example-contents"><p>
    この例では GET リクエストを想定しています。
    リクエストに POST フィールドが含まれていないため、
    <strong><code><a href="filter.constants.input.php#constant.input-post">INPUT_POST</a></code></strong> で指定された入力配列には値が入っておらず、
    配列の代わりに <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> が返されます。
    これはどの入力の種類にも当てはまります。
    クエリ文字列のないリクエストでは、
    <strong><code><a href="filter.constants.input.php#constant.input-get">INPUT_GET</a></code></strong> を指定した場合も <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> が返されます。
   </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">filter_input_array</span><span style="color: #007700">(</span><span style="color: #0000BB">INPUT_POST</span><span style="color: #007700">, [</span><span style="color: #DD0000">'a' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">]));
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>上の例の出力は、
たとえば以下のようになります。</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">NULL</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 notes" id="refsect1-function.filter-input-array-notes">
  <h3 class="title">注意</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    <strong><code><a href="filter.constants.input.php#constant.input-server">INPUT_SERVER</a></code></strong> 配列には、
    キー <code class="literal">REQUEST_TIME</code> が存在しません。
    あとで <var class="varname"><a href="reserved.variables.server.php" class="classname">$_SERVER</a></var> に追加されるからです。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.filter-input-array-seealso">
  <h3 class="title">参考</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.filter-input.php" class="function" rel="rdfs-seeAlso">filter_input()</a> - 指定した名前の変数を外部から受け取り、オプションでそれをフィルタリングする</span></li>
   <li><span class="function"><a href="function.filter-var.php" class="function" rel="rdfs-seeAlso">filter_var()</a> - 指定したフィルタでデータをフィルタリングする</span></li>
   <li><span class="function"><a href="function.filter-var-array.php" class="function" rel="rdfs-seeAlso">filter_var_array()</a> - 複数の変数を受け取り、オプションでそれらをフィルタリングする</span></li>
   <li>
    検証フィルタ
    <strong><code><a href="filter.constants.validation.php#constant.filter-validate-bool">FILTER_VALIDATE_<span class="replaceable">*</span></a></code></strong>
   </li>
   <li>
    除去フィルタ
    <strong><code><a href="filter.constants.sanitization.php#constant.filter-sanitize-string">FILTER_SANITIZE_<span class="replaceable">*</span></a></code></strong>
   </li>
  </ul>
 </div>


</div><?php manual_footer($setup); ?>