title: How to insert dynamic multidimensional array in database with mysqli
body: <p>I have the following array:</p>
  
  <pre><code>Array
  (
      [step1] =&gt; 3
      [step2] =&gt; Array
          (
              [1] =&gt; Array
                  (
                      [type] =&gt; 2
                      [price] =&gt; 312.5
                  )
  
              [0] =&gt; Array
                  (
                      [type] =&gt; 1
                      [price] =&gt; 51.5
                  )
  
          )
  
      [step3] =&gt; Array
          (
              [first_name] =&gt; Test
              [last_name] =&gt; Test
          )
  
      [step4] =&gt; Some answer
  )
  </code></pre>
  
  <p>Which I would like to Insert in a database. Step2 can have as many items as the user wants. As you might have noticed there are differences between the 2 arrays of step2. Also step3 can have a few more fields. This is all user dependent. </p>
  
  <p>The reason it is dynamic is because a user can manage form input fields in a dashboard.</p>
  
  <p>I tried to do a foreach loop for each step to insert but this didn't work really well. What I would like to do is the following: Have a query which is dynamic so each field can be inserted in the database. I think the easiest way to do this is to do a foreach loop on step2 so each item has the other values as well. My question is how can I insert all the data from this multidimensional array in a database with mysqli (OOP).</p>
  
  <p>Also checked a few other questions like this like:
  <a href="https://stackoverflow.com/questions/19463107/inserting-data-into-mysql-from-a-multidimensional-array-in-php">Inserting data into MySQL from a multidimensional array in php</a>,
  <a href="https://stackoverflow.com/questions/779986/insert-multiple-rows-via-a-php-array-into-mysql">insert multiple rows via a php array into mysql</a>,
  <a href="https://stackoverflow.com/questions/3960398/insert-php-array-into-mysql">insert php array into mysql</a>
  But these don't help me. So <code>implode()</code> is not going to help me. Also serialize is not going to work since each field has its own field in the database. (When a user edits the form in the dashboard fields in the database also get changed)</p>
  
  <p>Thanks in advance</p>