Answers.Body: <p>Two options here ...</p>
  
  <pre><code>{% set transformParams = { width: 1250, height: 1250 * aspectRatioSetting[entry.aspectRatio] } %}
  </code></pre>
  
  <p>Or just give the select field a value that represents the number, not the word. So set a value of 0.75 instead of four_three, and let the label be Four/Three (or whatever seems fit).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-26T20:04:10.673Z
Answers.Text: You have a great point about the select field value. I was using the value as a class-name in that I could style with CSS (since CSS has issues with numbers as class names). But I suppose I can just use the label for that and keep the value numeric as you say.
Answers.UserDisplayName: Dave Joyce
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-26T20:04:41.767Z
Answers.Text: However, going back to the original idea of using the key-value array, I tried what you suggested and I got a PHP warning: "array_key_exists(): The first argument should be either a string or an integer"
Answers.UserDisplayName: Dave Joyce
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-26T20:24:51.497Z
Answers.Text: What if you use `entry.aspectRatio.value` ?
Answers.UserDisplayName: Paul
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-27T14:31:30.217Z
Answers.Text: yup! That did the trick! Thanks :) Although I ended up going with your other suggestion, I now have a future trick in my book.
Answers.UserDisplayName: Dave Joyce
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-26T19:38:51.813Z
Answers.LastActivityDate: 2015-10-26T19:38:51.813Z
Answers.UserDisplayName: Paul
Answers.Body: <p>Try: </p>
  
  <p><code>{% set transformParams = { width: 1250, height: 1250 * (aspectRatioSetting.four_three|trim) } %}</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-26T20:27:04.290Z
Answers.Text: That's not exactly what I was looking to do originally, but adding trim helped for Paul's suggested solution of using number values in the dropdown field. So thanks!
Answers.UserDisplayName: Dave Joyce
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-26T20:31:10.223Z
Answers.Text: It's hackish, but ensures the types are the same.  More of a twig bug IMO.
Answers.UserDisplayName: Steve Adams
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-26T19:54:17.820Z
Answers.LastActivityDate: 2015-10-26T19:54:17.820Z
Answers.UserDisplayName: Steve Adams
Body: <p>This might be impossible, but can you lookup a value in a key-value array with the value in a craft entry field? And perform math with it in twig? I'm trying to simplify my template code.</p>
  
  <p>I have a key value array like so</p>
  
  <pre><code>{%
  set aspectRatioSetting = {
      'four_three' : '0.75',
      'three_two' : '0.666666666667',
      'sixteen_nine' : '0.5625',
      'one_one' : '1'
  } %}
  </code></pre>
  
  <p>And I'd like to be able to use those values in a math operation, the specific value looked up by a value in a custom entry field.</p>
  
  <pre><code>{% set transformParams = { width: 1250, height: 1250 * aspectRatioSetting.four_three } %}
  </code></pre>
  
  <p>where the value of the "four_three" key in the array is used in the math operation. Only the specific key would be set by the value in an entry field.</p>
  
  <pre><code>{% set transformParams = { width: 1250, height: 1250 * aspectRatioSetting.(entry.aspectRatio) } %}
  </code></pre>
  
  <p>There's not a good way for me to type it up because this clearly doesn't work. I'm just wondering if it's somehow possible. </p>
  
  <p>Or maybe I'm just overcomplicating things in an attempt to simplify things : /</p>
  
Tags: <entry><field><array>
Title: Can you lookup the value in a key-value array with the value in an entry field?
ThreadText: # Can you lookup the value in a key-value array with the value in an entry field?
  
  (Asked by: username_0 on 2015-10-26)
  
  This might be impossible, but can you lookup a value in a key-value array with the value in a craft entry field? And perform math with it in twig? I'm trying to simplify my template code.
  
  I have a key value array like so
  
  ```
  {%
  set aspectRatioSetting = {
      'four_three' : '0.75',
      'three_two' : '0.666666666667',
      'sixteen_nine' : '0.5625',
      'one_one' : '1'
  } %}
  
  ```
  
  And I'd like to be able to use those values in a math operation, the specific value looked up by a value in a custom entry field.
  
  ```
  {% set transformParams = { width: 1250, height: 1250 * aspectRatioSetting.four_three } %}
  
  ```
  
  where the value of the "four\_three" key in the array is used in the math operation. Only the specific key would be set by the value in an entry field.
  
  ```
  {% set transformParams = { width: 1250, height: 1250 * aspectRatioSetting.(entry.aspectRatio) } %}
  
  ```
  
  There's not a good way for me to type it up because this clearly doesn't work. I'm just wondering if it's somehow possible.
  
  Or maybe I'm just overcomplicating things in an attempt to simplify things : /
  
  **Tags:** entry, field, array
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-10-26. Score: 3 | Accepted answer
  
  Two options here ...
  
  ```
  {% set transformParams = { width: 1250, height: 1250 * aspectRatioSetting[entry.aspectRatio] } %}
  
  ```
  
  Or just give the select field a value that represents the number, not the word. So set a value of 0.75 instead of four\_three, and let the label be Four/Three (or whatever seems fit).
  
  > **Comments:**
  > **username_0**: You have a great point about the select field value. I was using the value as a class-name in that I could style with CSS (since CSS has issues with numbers as class names). But I suppose I can just use the label for that and keep the value numeric as you say.  
  > **username_0**: However, going back to the original idea of using the key-value array, I tried what you suggested and I got a PHP warning: "array_key_exists(): The first argument should be either a string or an integer"  
  > **username_1**: What if you use `entry.aspectRatio.value` ?  
  > **username_0**: yup! That did the trick! Thanks :) Although I ended up going with your other suggestion, I now have a future trick in my book.  
  
  ---
  
  ### Answer by username_2 on 2015-10-26. Score: 1
  
  Try:
  
  `{% set transformParams = { width: 1250, height: 1250 * (aspectRatioSetting.four_three|trim) } %}`
  
  > **Comments:**
  > **username_0**: That's not exactly what I was looking to do originally, but adding trim helped for username_1's suggested solution of using number values in the dropdown field. So thanks!  
  > **username_2**: It's hackish, but ensures the types are the same.  More of a twig bug IMO.  
  
  ---
  
  