How to rebuild assocciated array in PHP
<p>So I'm working with Laravel and the Laravel Excel extension. I want to import Excel file and than insert all data from the file to the MySQL DB. I know count and names of the DB table columns, but count and names of the Excel file maybe diffirent, so I deside users will be specified nesseccary column names in the Excel file for DB. For example we have DB table with a columns which names are "name", "cost" and "description" so if I have a file with the columns "column1" which equal "name" I should fill "name" field in the form as "column1" and I must do a same thing with other colums, so I had been faced with a surprise problem. There are my code with a comments:</p>

<pre><code>       Excel::load(Input::file('excel_import_file'), function($reader) {

            echo '&lt;pre&gt;';
            var_dump($reader-&gt;toArray());
            echo '&lt;/pre&gt;';

           //$reader-&gt;each(function($sheet) {

               //echo '&lt;pre&gt;';
               //var_dump($sheet-&gt;toArray());
               //echo '&lt;/pre&gt;';

               $input = Input::all(); //get all data from the input fields
               $fields = [
                   'subcategory_id' =&gt; $input['subcategory_id'],
                   'seller_id' =&gt; Auth::user()-&gt;id,
                   'name' =&gt; null,
                   'description' =&gt; null,
                   'cost' =&gt; null,
                   'currency_id' =&gt; $input['currency_id'],
                   'warranty' =&gt; null,
                   'img' =&gt; null,
                   'date_expiration' =&gt; null
               ]; //prepare nesseccary data for DB table with a foreighn keys (subcategory_id, currency_id are coming from the drop-down lists, so seller_id is comming from the session)


               $new_fields = []; //prepare new assocciated array for inserting to DB

                foreach($reader-&gt;toArray() as $a) { //parsing data
                    foreach($a as $k =&gt; $v) { //parsing each row from the Excel file as key =&gt; value

                        foreach($input as $input_k =&gt; $input_v) {

                            $k == $input_v ? $k = $input_k : $k = $k; //replacing keys in array from the Excel file for correct DB table structure

                        }

                        //echo $k . ' ' . $v . '&lt;br&gt;';



                        foreach($fields as $field_k =&gt; $field_v) {

                            $field_k == $k ? $field_v = $v : $field_v = $field_v; //looking for same keys from $fields array and assigning values for each key which exists in $k

                            echo $field_k . ' - ' . $field_v . '&lt;br&gt;';

                            $new_fields = array_merge($new_fields, [$field_k =&gt; $field_v]); //I catch a problem here, I want to merge two assoc arrays, but I got array with a first value and a second with a second value

                            //SAGoodies::insertTo($new_fields);


                        }



                        //echo '&lt;pre&gt;';
                        //var_dump($sheet-&gt;toArray());
                        //echo '&lt;/pre&gt;';

                        echo '&lt;pre&gt;';
                        var_dump($new_fields);
                        echo '&lt;/pre&gt;';

                    }
                }

           //});
        });
</code></pre>

<p>So here are a dump:</p>

<pre><code>subcategory_id - 43
seller_id - 20
name - ololo
description -
cost -
currency_id - 1
warranty -
img -
date_expiration -

array(9) {
  ["subcategory_id"]=&gt;
  string(2) "43"
  ["seller_id"]=&gt;
  int(20)
  ["name"]=&gt;
  string(5) "ololo"
  ["description"]=&gt;
  NULL
  ["cost"]=&gt;
  NULL
  ["currency_id"]=&gt;
  string(1) "1"
  ["warranty"]=&gt;
  NULL
  ["img"]=&gt;
  NULL
  ["date_expiration"]=&gt;
  NULL
}

subcategory_id - 43
seller_id - 20
name -
description -
cost - 333
currency_id - 1
warranty -
img -
date_expiration -

array(9) {
  ["subcategory_id"]=&gt;
  string(2) "43"
  ["seller_id"]=&gt;
  int(20)
  ["name"]=&gt;
  NULL
  ["description"]=&gt;
  NULL
  ["cost"]=&gt;
  float(333)
  ["currency_id"]=&gt;
  string(1) "1"
  ["warranty"]=&gt;
  NULL
  ["img"]=&gt;
  NULL
  ["date_expiration"]=&gt;
  NULL
}
</code></pre>

<p>But I want to got something like this:</p>

<pre><code>array(9) {
  ["subcategory_id"]=&gt;
  string(2) "43"
  ["seller_id"]=&gt;
  int(20)
  ["name"]=&gt;
  string(2) "ololo"
  ["description"]=&gt;
  NULL
  ["cost"]=&gt;
  float(333)
  ["currency_id"]=&gt;
  string(1) "1"
  ["warranty"]=&gt;
  NULL
  ["img"]=&gt;
  NULL
  ["date_expiration"]=&gt;
  NULL
}
</code></pre>

<p>I guess It happend because in this line:</p>

<pre><code>$new_fields = array_merge($new_fields, [$field_k =&gt; $field_v]);
</code></pre>

<p>we have everytime empty $new_fields. Any ideas?!</p>