繁体   English   中英

如何在PHP中重建关联数组

[英]How to rebuild assocciated array in PHP

因此,我正在使用Laravel和Laravel Excel扩展。 我想导入Excel文件,然后将文件中的所有数据插入MySQL数据库。 我知道数据库表列的计数和名称,但是Excel文件的计数和名称可能有所不同,因此,我请用户在数据库的Excel文件中指定nesseccary列名称。 例如,我们有一个DB表,其中的列名为“ name”,“ cost”和“ description”,因此,如果我有一个列“ column1”等于“ name”的文件,则应在表单中填写“ name”字段作为“ column1”,我必须与其他column做同样的事情,因此我面临着一个令人惊讶的问题。 我的代码带有注释:

       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>';

                    }
                }

           //});
        });

所以这是一个转储:

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
}

但是我想要这样的东西:

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
}

我猜发生了,因为在这一行:

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

我们每次都有空的$ new_fields。 有任何想法吗?!

因此,我通过Laravel Excel的选择方法找到了新的解决方案。 看起来像

    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>';
        }

    });

这个解决方案有什么想法? 从$ result得到的数组中的想法。 我不想碰它的结构,不想重复。 我只是将来自$ fields的必需的外键添加到$ result数组中的每个数组。 简单有效的解决方案。

Ps比我需要请求格式和检查功能来检查加载文件的扩展名,它必须是xls,xlsx或csv并检查文件大小以防止现在加载太大的文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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