title: Populating database field with multiple value
body: <p>I am working on populating the database tables. The table have fields which some of them are enum. </p>
  
  <p>Consider a user which have a field <code>status</code> , whose values can be <code>active</code>, <code>inactive</code> etc. Assume we can modify the configuration values and running the script the data can be populated accordingly.</p>
  
  <p>Let us represent the <code>user</code> table whose <code>status</code> field as </p>
  
  <pre><code>'status' =&gt; array(
     'active' =&gt; 3, 
     'inactive', 
     'deleted',
  ),
  </code></pre>
  
  <p>In this case assume we need to create 3 users with <code>status</code> , <code>active</code>. 1 user with status <code>inactive</code> and 1 with <code>deleted</code>.</p>
  
  <p>The table may be having more enum fields. So the config can expand. Depending on the configuration and fields the values will be multiples. </p>
  
  <p>Consider the below example.</p>
  
  <p>Eg : </p>
  
  <pre><code>$config = array(
      'table1name' =&gt; array(
          'field1' =&gt; array(
              'active' =&gt; 3, 
              'inactive', 
              'deleted',
          ),
          'field2' =&gt; array(
              'admin', 
              'user', 
              'editor'
          ),
          ....,
          'more-fields' =&gt; array(
              'more-values', 
          )
      ),
      'table2name' =&gt; array(
          'field1' =&gt; array(
              'active', 
              'inactive', 
              'deleted',
          ),
      )
  );
  </code></pre>
  
  <p>In this case there need to populate <code>table1</code> whose field <code>field1</code> with <code>active</code>, <code>inactive</code>, <code>deleted</code> and <code>roles</code> with <code>admin</code>, <code>user</code>, <code>editor</code> etc. ( The active, inactive etc are provided just for example. It can be just values. )</p>
  
  <p>The idea is to generate more users depending on the count if any provided.</p>
  
  <p>Eg :</p>
  
  <pre><code>'status' =&gt; array(
      'active' =&gt; 10, 
      'inactive' =&gt; 2, 
      'deleted' =&gt; 3,
  ),
  'roles' =&gt; array(
      'admin' =&gt; 2, 
      'user', 
      'editor'
  )
  ....,
  'more-fields' =&gt; array(
      'more-values', 
  )
  </code></pre>
  
  <p>So that there will be </p>
  
  <p>10 * 4 => active users (10 * 2 active admin  / 10 active user, 10 active editor ) + 
  2 * 4 => inactive users ( 2 inactive admin , 1 user, 1 editor ) + 
  3 * 4 => deleted users in total. </p>
  
  <p>I am struggling to build the algorithm for the same.</p>
  
  <pre><code>array(
      'status' =&gt; array(
          'active' =&gt; 10, 
          'inactive' =&gt; 2, 
          'deleted' =&gt; 3,
      ),
      'roles' =&gt; array(
          'admin' =&gt; 2, 
          'user', 
          'editor'
      ),
      ....,
      'more-fields' =&gt; array(
          'more-values', 
      )
  )
  
  // In this example you can see we have not covered the fields of the table when they are more than 1 on save.It looks we need to build the array with values first.
  
  foreach ($config as $table =&gt; $fields) {
      foreach ($fields as $field =&gt; $values ) {
          foreach ($values as $key =&gt; $statusCount) {
              if (is_string($key)) {
                  $model = new User();
                  $model-&gt;$field = $key;
                  $model-&gt;another = 'value';
                  $model-&gt;save();
              } else {            
                  for ($i = 0; $i&lt; $statusCount; $i++) {
                      $model = new User();
                      $model-&gt;$field = $key;
                      $model-&gt;another = 'value';
                      $model-&gt;save();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>UPDATE : </p>
  
  <p>Changes made according to @the-fourth-bird answer <a href="https://stackoverflow.com/a/33354032/487878">https://stackoverflow.com/a/33354032/487878</a></p>
  
  <p>Problem is it only look for 2 fields, the fields can be 1 or n.</p>