Answers.Body: <p>You can use the <code>Craft::configure()</code> for that.. imagine your array looks like</p>
  
  <pre><code>$criteria = [
      'age' =&gt; ['and', '&gt;=30', '&lt;=50'],
      'enabled' =&gt; false
      'status' =&gt; null,
  ]
  </code></pre>
  
  <p>You can do</p>
  
  <pre><code>$query = User::find();
  Craft::configure($query, $criteria);
  
  $users = $query-&gt;all();
  </code></pre>
  
  <p>Or you can just use the parameter based syntax instead of the functional one </p>
  
  <pre><code>$users-&gt;$key = $value;
  </code></pre>
  
  <p>You'll receive a deprecated error when you don't fetch your users. Your so called <code>$users</code> variable hast actually nothing to do with <code>users</code> it's a really bad name because one might think <code>users</code> store information about a list of users but that's not the case. <code>$users</code> is a <code>Query</code> that looks for <code>elements</code> with the type <code>craft\elements\user</code> in your database. You'll receive your list of <code>users</code> when you execute your <code>Query</code> with <code>one()</code>, <code>all()</code> or something like that.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-28T23:48:00.960Z
Answers.Text: I'll give this a shot and report back. Thanks, Robin!
Answers.UserDisplayName: Mark J. Reeves
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-28T12:52:20.033Z
Answers.LastActivityDate: 2018-04-28T12:57:34.890Z
Answers.LastEditDate: 2018-04-28T12:57:34.890Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I'm trying to dynamically create an element query in PHP where the custom field criteria come from an array of strings. Something like this...?</p>
  
  <pre><code>$users = User::find();
  $users-&gt;group($groupList);
  
  foreach ($criteria as $key =&gt; $value) {
      // Each key value is the handle for a custom field and the
      // field value used as criteria
      $users-&gt;$key($value);
  }
  
  $users-&gt;all();
  </code></pre>
  
  <p>How can I use a custom field name in a string as a parameter?</p>
  
  <p><strong>Edit</strong> If I don't chain the criteria, I also get a deprecation error when I iterate over <code>$users</code>...Is there a better way to do that, too?</p>
  
Tags: <plugin-development><craft3><elementcriteriamodel><php><elementquery>
Title: How to create an element query in PHP with custom field names in variables?
ThreadText: # How to create an element query in PHP with custom field names in variables?
  
  (Asked by: username_0 on 2018-04-27)
  
  I'm trying to dynamically create an element query in PHP where the custom field criteria come from an array of strings. Something like this...?
  
  ```
  $users = User::find();
  $users->group($groupList);
  
  foreach ($criteria as $key => $value) {
      // Each key value is the handle for a custom field and the
      // field value used as criteria
      $users->$key($value);
  }
  
  $users->all();
  
  ```
  
  How can I use a custom field name in a string as a parameter?
  
  **Edit** If I don't chain the criteria, I also get a deprecation error when I iterate over `$users`...Is there a better way to do that, too?
  
  **Tags:** plugin-development, craft3, elementcriteriamodel, php, elementquery
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-04-28. Score: 3 | Accepted answer
  
  You can use the `Craft::configure()` for that.. imagine your array looks like
  
  ```
  $criteria = [
      'age' => ['and', '>=30', '<=50'],
      'enabled' => false
      'status' => null,
  ]
  
  ```
  
  You can do
  
  ```
  $query = User::find();
  Craft::configure($query, $criteria);
  
  $users = $query->all();
  
  ```
  
  Or you can just use the parameter based syntax instead of the functional one
  
  ```
  $users->$key = $value;
  
  ```
  
  You'll receive a deprecated error when you don't fetch your users. Your so called `$users` variable hast actually nothing to do with `users` it's a really bad name because one might think `users` store information about a list of users but that's not the case. `$users` is a `Query` that looks for `elements` with the type `craft\elements\user` in your database. You'll receive your list of `users` when you execute your `Query` with `one()`, `all()` or something like that.
  
  > **Comments:**
  > **username_0**: I'll give this a shot and report back. Thanks, username_1!  
  
  ---
  
  