简体   繁体   中英

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?!

So, I found new fresh solution by select method of the Laravel Excel. It looks like

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

        $input = Input::all();

        $result = $reader->select(array($input['name'],
                                        $input['description'],
                                        $input['cost'],
                                        $input['warranty'],
                                        $input['img'],
                                        $input['date_expiration']))->limit(500)->get();

        $fields = [
            'subcategory_id' => $input['subcategory_id'],
            'seller_id' => Auth::user()->id,
            'currency_id' => $input['currency_id'],
        ];

        $new_array = [];

        foreach($result->toArray() as $array) {
            $new_array = array_merge($array, $fields);

            SAGoodies::insertTo($new_array);

            //echo '<pre>';
            //var_dump($new_array);
            //echo '</pre>';
        }

    });

What's an idea of this solution?! Idea in array which we are getting from $result. I don't want to touch a sctucture of It, don't want to iterating. I just add required foreighn keys from $fields to each array in $result array. Simple and effectivity solution.

Ps than I need to make request for the form and check function for checking an extension of the loading files, It must be xls, xlsx or csv and checking file size to prevent loading too big files now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM