Answers.Body: <p>I wouldn't consider it as good practice to do what you want, there are good reasons why something like this is not possible by default and why all functions to get all parameters are private.</p>
  
  <p>A better way is to send your parameters inside an array and fetch this or you can create an array of all possible parameters and loop it</p>
  
  <pre class="lang-php prettyprint-override"><code>$secureParameters = ['A', 'B', 'C'];
  $request = Craft::$app-&gt;getRequest();
  $values = [];
  foreach($secureParameters as $key){
      $values[$key] = $request-&gt;getBodyParam($key);
  }
  </code></pre>
  
  <p>If you really don't care about receiving unexpected parameters and raising exceptions you can also loop your <code>$_POST</code> directly <code>foreach($_POST as $key =&gt; $v){</code> and fetch the <code>$request-&gt;getBodyParam($key)</code> at least</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-09T09:07:31.303Z
Answers.LastActivityDate: 2018-08-09T09:07:31.303Z
Answers.UserDisplayName: Robin Schambach
Body: <p>Currently, I am doing this.</p>
  
  <pre><code>$responses = array(
          "E" =&gt; 0,
          "I" =&gt; 0);
  
  foreach ($_POST as $key =&gt; $value) {
      if (strlen($value) == 1) {
          ++$responses[$value];
      }
  }
  </code></pre>
  
  <p>Is there a way to use craft()->requests for this? I tried</p>
  
  <pre><code>foreach (craft()-&gt;request-&gt;getPost() as $key =&gt; $value) {
      if (strlen($value) == 1) {
          ++$responses[$value];
      }
  }
  </code></pre>
  
  <p>But this did not seem to work.</p>
  
Tags: <plugin-development><craft3>
Title: Is there a safer way to Cycle Through Post Data in Craft
ThreadText: # Is there a safer way to Cycle Through Post Data in Craft
  
  (Asked by: username_0 on 2018-08-09)
  
  Currently, I am doing this.
  
  ```
  $responses = array(
          "E" => 0,
          "I" => 0);
  
  foreach ($_POST as $key => $value) {
      if (strlen($value) == 1) {
          ++$responses[$value];
      }
  }
  
  ```
  
  Is there a way to use craft()->requests for this? I tried
  
  ```
  foreach (craft()->request->getPost() as $key => $value) {
      if (strlen($value) == 1) {
          ++$responses[$value];
      }
  }
  
  ```
  
  But this did not seem to work.
  
  **Tags:** plugin-development, craft3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-08-09. Score: 3
  
  I wouldn't consider it as good practice to do what you want, there are good reasons why something like this is not possible by default and why all functions to get all parameters are private.
  
  A better way is to send your parameters inside an array and fetch this or you can create an array of all possible parameters and loop it
  
  ```php
  $secureParameters = ['A', 'B', 'C'];
  $request = Craft::$app->getRequest();
  $values = [];
  foreach($secureParameters as $key){
      $values[$key] = $request->getBodyParam($key);
  }
  
  ```
  
  If you really don't care about receiving unexpected parameters and raising exceptions you can also loop your `$_POST` directly `foreach($_POST as $key => $v){` and fetch the `$request->getBodyParam($key)` at least
  
  
  ---
  
  