donglu7286 2016-08-25 10:11
浏览 38

如何在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?!

  • 写回答

1条回答 默认 最新

  • douluanzhao6689 2016-08-25 12:33
    关注

    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.

    P.s. 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.

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制