<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>

CC BY-SA 4.0
2018-08-09T09:07:31.303Z
2018-08-09T09:07:31.303Z
Robin Schambach
<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>

<plugin-development><craft3>
Is there a safer way to Cycle Through Post Data in Craft
# 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
$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


---

