jeudi 25 août 2016

How to rebuild assocciated array in PHP

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:

       Excel::load(Input::file('excel_import_file'), function($reader) {

            echo '<pre>';
            var_dump($reader->toArray());
            echo '</pre>';

           //$reader->each(function($sheet) {

               //echo '<pre>';
               //var_dump($sheet->toArray());
               //echo '</pre>';

               $input = Input::all(); //get all data from the input fields
               $fields = [
                   'subcategory_id' => $input['subcategory_id'],
                   'seller_id' => Auth::user()->id,
                   'name' => null,
                   'description' => null,
                   'cost' => null,
                   'currency_id' => $input['currency_id'],
                   'warranty' => null,
                   'img' => null,
                   'date_expiration' => 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->toArray() as $a) { //parsing data
                    foreach($a as $k => $v) { //parsing each row from the Excel file as key => value

                        foreach($input as $input_k => $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 . '<br>';



                        foreach($fields as $field_k => $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 . '<br>';

                            $new_fields = array_merge($new_fields, [$field_k => $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 '<pre>';
                        //var_dump($sheet->toArray());
                        //echo '</pre>';

                        echo '<pre>';
                        var_dump($new_fields);
                        echo '</pre>';

                    }
                }

           //});
        });

So here are a dump:

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

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

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

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

But I want to got something like this:

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

I guess It happend because in this line:

$new_fields = array_merge($new_fields, [$field_k => $field_v]);

we have everytime empty $new_fields. Any ideas?!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire